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