views:

328

answers:

2

I am developing an iPad application, and I am trying to figure out the best way to decide if a user can connect to the Internet. If the user has no connectivity, I will load cached data, otherwise I will load new data. I am trying to use Apple's reachability class for this, and I wanted to see if I am doing this correctly. In applicationDidFinishLaunchingWithOptions, I am doing this:

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

Reachability hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReach startNotifer];

Then my reachabilityChanged: looks like this:

- (void)reachabilityChanged:(NSNotification* )note {
    Reachability *curReach = [note object];
    self.internetConnectionStatus = [curReach currentReachabilityStatus];

    if (internetConnectionStatus == NotReachable) {
        [viewController getDataOffline];
    } else {
        if (![[NSUserDefaults standardUserDefaults] objectForKey:kFIRST_LAUNCH]) [viewController getCurrentLocation];
        else [viewController getData];
    }
}

Right now, this is working perfectly for WiFi iPads. I just want to make sure that this will work for 3G iPads. Could you please let me know if I am doing this correctly or not?

+1  A: 

Yes, the Reachability class will tell you if you can reach it using any network method.

Jeff Kelley
+2  A: 

Yes, the reachability class can use both WiFi and 3G to determine if a remote host is reachable.

Also as a side note you may want to consider always displaying cached data on launch of your application, then asynchronously updating to the new data in the background. Depending on the context of your application this could create a much better user experience since some data is always available.

If you are interested in learning more about this there is a fantastic chapter titled "Fake It ’Til You Make It: Tips and Tricks for Improving Interface Responsiveness" found in the "iPhone Advanced Projects" book.

Dillan