I'm trying to get place details by giving place id in the url as shown below.
func findingPlaceDetailsFromPlaceId(id : String){
var urlString = "https://maps.googleapis.com/maps/api/place/details/json?placeid=\(id)&key=\(apiServerKey)"
urlString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
//println(urlString)
if placesTask.taskIdentifier > 0 && placesTask.state == .Running {
placesTask.cancel()
}
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {data, response, error in
println(error)
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
if let json = NSJSONSerialization.JSONObjectWithData(data, options:nil, error:nil) as? NSDictionary {
if let results = json["result"] as? NSArray {
println(results.count)
for rawPlace:AnyObject in results {
let dict = rawPlace as! NSDictionary
if let location = dict["geometry"]?["location"] as? NSDictionary{
let lat = location["lat"] as! CLLocationDegrees
let long = location["lng"] as! CLLocationDegrees
self.coordinate = CLLocationCoordinate2DMake(lat, long)
}
}
}
}
dispatch_async(dispatch_get_main_queue()) {
}
}
self.placesTask.resume()
}
The given url is a valid one. If I put the same url in google I'm getting valid results, but when I run it in my app it's showing "Error Domain=NSURLErrorDomain Code=-999 "cancelled".
I found the solution for my above error. I was calling findingPlaceDetailsFromPlaceId(id : String) method from the didSelectRow of the searchResultControllerView. As soon after calling the method I was dismissing the searchResultControllerView. It was leading to above error. Now am dismissing the searchResultControllerView in dispatch_async(dispatch_get_main_queue()) like shown below.
func findingPlaceDetailsFromPlaceId(id : String){
var urlString = "https://maps.googleapis.com/maps/api/place/details/json?placeid=\(id)&key=\(apiServerKey)"
urlString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
if placesTask.taskIdentifier > 0 && placesTask.state == .Running {
placesTask.cancel()
}
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {data, response, error in
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
if let json = NSJSONSerialization.JSONObjectWithData(data, options:nil, error:nil) as? NSDictionary {
if let results = json["result"] as? NSDictionary {
if let location = results["geometry"]?["location"] as? NSDictionary{
let lat = location["lat"] as! CLLocationDegrees
let long = location["lng"] as! CLLocationDegrees
self.coordinate = CLLocationCoordinate2DMake(lat,long)
}
}
}
//This is what I added
dispatch_async(dispatch_get_main_queue()) {
//Dismissing the ResultControllerView
self.searchController?.dismissViewControllerAnimated(true, completion: {
//Pushing the view back to main view after completion
performSegueWithIdentifier("unwindFromSelectLocation", sender: self)
})
}
}
Problem solved.. Hope it will be usefull for others!!!