Saturday, March 28, 2015

Unsupported URL iOS

So I had a task to download some locations from google maps api. Task is simple - generate url string:
https://maps.googleapis.com/maps/api/place/radarsearch/json?location=27.5235,31.21111&radius=30000&types=dentist|hospital|pharmacy&key=FAKEKEY69_41gOViVF_v06xplaHcMl_py2jPI
and download any possible locations from it. It works if you put it in web browser. But if I try to download data from iOS Application - it returns error:
Error description=Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x78f97920 {NSUnderlyingError=0x79f78bd0 "unsupported URL", NSLocalizedDescription=unsupported URL}
It turns out - the reason is url after all. It contains some illegal characters and it needs to be adjusted, before attempting to download data.
NSString *path = @"https://maps.googleapis.com/maps/api/place/radarsearch/json?location=27.5235,31.21111&radius=30000&types=dentist|hospital|pharmacy&key=FAKEKEY69_41gOViVF_v06xplaHcMl_py2jPI";
NSString *finalPath = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
And final url string looks like this:
https://maps.googleapis.com/maps/api/place/radarsearch/json?location=27.5235,31.21111&radius=30000&types=dentist%7Chospital%7Cpharmacy&key=FAKEKEY69_41gOViVF_v06xplaHcMl_py2jPI