来自太多请求的NSURLSession和地理编码器错误,解决方案是什么?

时间:2016-04-09 06:08:59

标签: ios xcode swift nsurlsession

每当用户使用我的应用进行搜索时,它都会返回最多100个结果的数组。我需要对每个索引进行地理编码(我在每个索引处都有lat和lon),并在地图上放置一个标记。

我还需要使用NSURLSession显示https。然后,我在指定的标记位置显示每个图像。一切正常,#34;罚款"对于较低的数字,但我得到两个重大错误:

1)对于图像内容:

NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)

2)对于地理编码器:

geoCoder error probably too many requests Optional(Error Domain=kCLErrorDomain Code=2 "(null)")

我的猜测是我做了太多请求,苹果有限制。那我怎么能解决这个问题呢?当然有一种方法可以做到这一点,或者可能是一种不同的方法?以下是显示图像的代码:

extension UIImageView {
    public func imageFromUrl(urlString: String) {
        if let url = NSURL(string: urlString) {
            let request = NSURLRequest(URL: url)
            NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
                (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in
                if let imageData = data as NSData? {
                    self.image = UIImage(data: imageData)
                }
            }
        }
    }
}

以下是地理编码器的代码:

for i in (0 ..< tbc.categorizedArray.count) {

                //make sure that the specified index actually has a geoLon and geoLat
                if var longitude :Double = tbc.categorizedArray[i]["geoLon"] as? Double {
                    longitude = tbc.categorizedArray[i]["geoLon"] as! Double
                    let latitude :Double = tbc.categorizedArray[i]["geoLat"] as! Double

                    //if it does, save it's location
                    let location = CLLocation(latitude: latitude, longitude: longitude)

                    //reverseGeoCode it's location
                    CLGeocoder().reverseGeocodeLocation(location, completionHandler: {
                        placemarks, error in
                        if error != nil {
                            print("geoCoder error probably too many requests \(error)")
                            return
                        }

                        //place the markers
                        if let placemarks = placemarks {
                            let placemark = placemarks[0]
                            // Add annotation
                            let annotation = MKPointAnnotation()
                            annotation.title = tbc.categorizedArray[i]["screenname"] as? String
                            annotation.subtitle = tbc.categorizedArray[i]["userPost"] as? String
                            if let location = placemark.location {
                                annotation.coordinate = location.coordinate
                                // Display the annotation
                                self.mapView.showAnnotations([annotation], animated: true)
                                self.mapView.selectAnnotation(annotation, animated: true)
                            }
                        }
                    })
                }

1 个答案:

答案 0 :(得分:1)

-9802是App Transport Security问题。检查你的证书。

不知道地理编码器问题。