错误域= NSURLErrorDomain代码= -999"已取消"

时间:2015-04-16 22:31:29

标签: ios swift google-places-api google-maps-sdk-ios

我试图通过在网址中提供地点ID来获取地点详细信息,如下所示。

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

给定的网址是有效网址。如果我在谷歌中添加相同的网址我会获得有效的结果,但是当我在我的应用中运行它时,它会显示"错误域= NSURLErrorDomain代码= -999"已取消&#34 ;

1 个答案:

答案 0 :(得分: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)
            })



        }

    }

问题解决了..希望它对别人有用!!!