CLGeocoder如何设置超时?

Ras*_*sim 9 objective-c ios

我试图通过CLLocation结构定义城市名称,反向...方法请求CLGeocoder对象,但我不知道如何设置超时?如果没有互联网连接请求时间可能需要大约30秒 - 这太长了...

小智 5

据我所知,没有任何内置功能可以处理此问题。我已经开始使用以下解决方案。抱歉,它是Swift语言,我不会说流利的Objective-C。

这会给您2秒钟的超时时间

func myLookupFunction(location: CLLocation)
{
    let timer = NSTimer(timeInterval: 2, target: self, selector: "timeout:", userInfo: nil, repeats: false);
     geocoder.reverseGeocodeLocation(location){
        (placemarks, error) in
        if(error != nil){
          //execute your error handling
        }
        else
        {
          //execute your happy path
        }
    }
    NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
}

func timeout(timer: NSTimer)
{
    if(self.geocoder.geocoding)
    {
        geocoder.cancelGeocode();
    }
}
Run Code Online (Sandbox Code Playgroud)

超时触发后,将执行您的回调并调用错误路径。

您会收到以下错误信息:

  • 代码= 10
  • 本地化描述(英语)=操作无法完成。(kCLErrorDomain错误10。)

希望这可以帮助。