CLGeocoder如何设置超时?

时间:2013-10-18 07:07:59

标签: ios objective-c

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

1 个答案:

答案 0 :(得分:1)

@Swango18回答Swift 3:

func myLookupFunction(location: CLLocation)
{
    let timer = Timer(timeInterval: 2, target: self, selector: #selector(self.timeout), userInfo: nil, repeats: false);
    geocoder.reverseGeocodeLocation(location){
        (placemarks, error) in
        if(error != nil){
            //execute your error handling
        }
        else
        {
            //execute your happy path
        }
    }
    RunLoop.current.add(timer, forMode: RunLoopMode.defaultRunLoopMode)
}

func timeout()
{
    if (geocoder.isGeocoding){
        geocoder.cancelGeocode()
    }
}