How to Deal with Network Failure When Using Maps in Your iOS App - dummies

How to Deal with Network Failure When Using Maps in Your iOS App

By Neal Goldstein, Dave Wilson

What if the Internet isn’t available for your special map to work on your iOS app? The Apple Human Interface Guidelines (and hopefully, common sense) say that app programmers should keep the user informed of what’s going on.

By virtue of the fact that you’ve made the MapController an MKMapView delegate, your app is in the position to send a message in the event of a load failure, because if a failure occurs, you’re notified, provided you implement the mapViewDidFailLoadingMap: method. You can respond to a load failure by adding the code MapController.m.

- (void)mapViewDidFailLoadingMap:(MKMapView *)mapView
withError:(NSError *)error {
NSLog(@"Unresolved error %@, %@", error,
[error userInfo]);
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Unable to load the map"
message:@"Check to see if you have internet access"
delegate:self cancelButtonTitle: @"Thanks"
[alert show];

Testing this failure business requires disconnecting from the Internet after you have launched your app. And because of map caching, you might have to pan the map to get the warning. The simplest way to disconnect from the Internet is to turn Airplane Mode on. That’s probably what will happen to your user most often anyway.