3

My iPhone app requires internet to function.

Should I checks for connectivity everytime a internet function is executed? Or should I use my appDelegate to listen for messages from something like the reachability class to determine if the connection was lost and warn the user?

What is better?

Here's what I've done so far in my app delegate:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];  

    reachability = [[Reachability reachabilityWithHostName:[NSString stringWithFormat:@"http://%@/iSql/" ,[[NSUserDefaults standardUserDefaults] stringForKey:@"serviceIPAddress"]]] retain];

    [reachability startNotifier];

    return YES;
}

- (void)reachabilityChanged:(NSNotification *)note {

    NetworkStatus ns = [(Reachability *)[note object] currentReachabilityStatus];

    if (ns == NotReachable) {

        if (![networkAlert isVisible]) {

            if ([self networkAlert] == nil) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"\n\nNo Internet Connection" message:@"You require an internet connection to retrieve data from the server." delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
                [self setNetworkAlert:alert];
                [alert release];
            }

            [networkAlert show];  
        }
    } else {

        if ([self networkAlert] != nil) {
            [networkAlert dismissWithClickedButtonIndex:0 animated:YES]; 
        }

    }
}
Hackmodford
  • 3,901
  • 4
  • 35
  • 78

4 Answers4

4

At least one Apple tech talk presenter recommended not checking Reachability in most cases, just firing off your async network requests, informing the user (via some type of activity indicator) that the app is waiting for a network response, and giving the user an option if in their opinion they've waited long enough, instead of locking up the UI or the app.

The reason is that, on a moving mobile device, the network can and will often go up between the Reachability tests and any actual data requests. Thus the user will be misinformed. The net can also go down after informing the user that there is connectivity, which can even be worse.

Plus Reachability only tells you about the connectivity of the nearest/1st network hop, which may or may not provide connectivity to the rest of the internet or to your destination site. Common example might be all those misconfigured WIFI access points.

Locking up the UI, and not giving the user any option after waiting long enough is likely grounds for rejection, whether or not you've checked Reachability first.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • I like this answer - I was at a similar tech talk with Apple where this was recommended. I would not necessarily recommend blocking the entire app with a `UIAlertView` as per another answer, not very friendly in my opinion! – lxt Dec 28 '11 at 22:12
2

It really depends on your application. You could do both: disable functionality when the internet is not available and inform the user.

I prefer to change the UI when there is no internet connection, and make sure there is connectivity before firing off any methods that require a connection.

Abizern
  • 146,289
  • 39
  • 203
  • 257
0

I recommend:

1) Always check for internet connectivity prior to making any internet request and handle appropriately. 2) Have a polling mechanism that will check reachability. If unreachable, perhaps warn the user AND disable portions of your program that make internet requests. Maybe call it 'Offline Mode'.

Jeremy
  • 8,902
  • 2
  • 36
  • 44
  • The problem is my app is useless with no connection. I'd rather have a way of disabling the app from the appDelegate... maybe I can present the notification with no button and remove it only when the network is back... – Hackmodford Dec 28 '11 at 20:02
  • All the settings are handled in the phones settings app. My problem right now is how to dismiss the alert once it's been shown. since the alert is released after being shown. – Hackmodford Dec 28 '11 at 20:57
  • I want to halt ALL user interaction when there is no network because the app is completely useless... therefore a uialertview is perfect! – Hackmodford Dec 28 '11 at 21:10
  • 1
    Make your app useful when there is no Internet connection, If only by giving the user an option to see a help or info view or even an advertisement saying how fantastic your app is when there is a connection. – hotpaw2 Dec 29 '11 at 18:46
0

Checking for internet is technically up to you as the developer to decide, but Apple has in the past rejected apps for not notifying the users that the internet connection was not available.

http://mobileorchard.com/avoiding-iphone-app-rejection-from-apple/ (#6)

Flipper
  • 2,589
  • 3
  • 24
  • 32