Reputation: 1307
I'm using the following code to search for a place:
let correctedAddress:String! = self.searchResults[(indexPath as NSIndexPath).row].addingPercentEncoding(withAllowedCharacters: CharacterSet.symbols)
let url = URL(string: "https://maps.googleapis.com/maps/api/geocode/json?address=\(correctedAddress)&sensor=false")
self.searchResults[(indexPath as NSIndexPath).row] returns a string like this: "São Paulo - State of São Paulo, Brazil"
The string that is the parameter of URL is like this (after the encoding):
https://maps.googleapis.com/maps/api/geocode/json?address=Optional(\%22State%20of%20S%C3%A3o%20Paulo%2C%20Brazil\%22)&sensor=false
If I search for this URL in my web browser, the JSON is shown. So I assume it is a valid URL, but the URL(string: ..) method always returns nil
I probably need to change my encoding, I tried using CharacterSet.urlPathAllowed but always return nil.
What kind of encoding I should use? Or there is something else wrong?
Upvotes: 3
Views: 1135
Reputation: 531
This worked for me :
"https://maps.googleapis.com/maps/api/geocode/json?address=" + correctedAddress + "&sensor=false"
Upvotes: 0
Reputation: 2998
Try to remove the Optional(...) string in the url
https://maps.googleapis.com/maps/api/geocode/json?address=\%22State%20of%20S%C3%A3o%20Paulo%2C%20Brazil\%22&sensor=false
I believe that's the reason for the URL is returning nil
.
Upvotes: 4