Answer the question
In order to leave comments, you need to log in
I am using weatherapi.com init URL(string: urlString). The received link works in the browser, but the application does not receive data from it. What's the matter?
I am using weatherapi.com init URL(string: urlString). The received link works in the browser, but the application does not receive data from it. Code below
func ferchFromServer(cityName: String) {
let urlString = "https://api.weatherapi.com/v1/current.json?key=\(apikey!)&q=\(cityName)"
let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
print(url!)
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url!) { (data, response, error) in
if let receivedData = data {
if let datas = self.parseJSON(data: receivedData){
self.delegate?.updateInterface(self, datas: datas)
}
}
}
task.resume()
}
print(url) > https://api.weatherapi.com/v1/current.json?key=<MYKEY>&q=New%20York
The data couldn’t be read because it isn’t in the correct format.
Answer the question
In order to leave comments, you need to log in
Try not to use a space (%20) as a delimiter in the cityName variable. And use en dash
https://api.weatherapi.com/v1/current.json?key=&q=... returns correct data
https://api.weatherapi.com/v1/current.json?key=&q= ... also returns correct data, but it's better not to use + in the address bar)))
Where do you study?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question