2015-04-16 103 views
0

我試圖通過在URL中提供地點id來獲取地點詳細信息,如下所示。錯誤域= NSURLErrorDomain代碼= -999「取消」

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() 
} 

給定的url是有效的。如果我把相同的網址在谷歌我得到有效的結果,但是當我運行它在我的應用程序它顯示「錯誤域= NSURLErrorDomain代碼= -999」取消「

+2

您似乎已取消會話。如果placesTask.taskIdentifier> 0 && placesTask.state ==.Running { placesTask.cancel() } –

+0

請注意這兩行。這應該是真正的答案。 – gnasher729

回答

0

我找到了解決方案,我的上述錯誤 我在searchResultControllerView的didSelectRow中調用findPlaceDetailsFromPlaceId(id:String)方法,調用該方法後立即關閉了searchResultControllerView,導致上述錯誤,現在dispatch_async(dispatch_get_main_queue())中關閉searchResultControllerView。如下圖所示。

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) 
      }) 



     } 

    } 

問題解決了..希望這將是有用的人!

相關問題