CLGeocoder reverseGeocodeLocation results when no Internet connection is available

StackOverflow https://stackoverflow.com/questions/15091980

  •  15-03-2022
  •  | 
  •  

I am seeing odd results when trying to reverse geocode a location if the network is not available:

  • seen on iOS 5.0.1
  • Airplane mode ON

    [geocoder reverseGeocodeLocation:whatever completionHandler:^(NSArray *placemarks, NSError *error) {
    if (error) 
    {
         // Alert(@"No Internet connection")!;
    
         // I should get a kCLErrorNetwork, but I get a kCLErrorGeocodeFoundPartialResult
         // with a placemark array containing a CLPlacemark *
         // with only a latitude and longitude (the ones I entered)...
    }
    else
    {
         // ...
    }
    

This is not documented; at least I couldn't find it.

It is not a big issue per se, since I can't expect it to properly reverse geocode an address without network, but it prevents me from displaying an informative message to the user.

有帮助吗?

解决方案

That's an iOS 5.0.x behavior. In 5.1 and later, it turns kCLErrorNetwork as you would have expected. If you use the macros from https://stackoverflow.com/a/5337804/1271826 you could theoretically do something like:

[geocoder reverseGeocodeLocation:whatever completionHandler:^(NSArray *placemarks, NSError *error) {

    if (error) 
    {
         if (error.code == kCLErrorNetwork || (error.code == kCLErrorGeocodeFoundPartialResult && SYSTEM_VERSION_LESS_THAN(@"5.1")))
         {
             Alert(@"No Internet connection!");
         }
    }
    else
    {
         // ...
    }
}];

That way, you'll handle the network error regardless of what iOS version the user is running (though, obviously, only 5.0 and later, given that CLGeocoder was introduced in iOS 5.0).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top