开发者

iPhone app, alert users when they aren't connected to the internet?

What's an easy way to code an alert that warns users when they aren't connected to the Internet? 开发者_C百科I'm using Xcode, right now when there is no connection it is just a white screen in uiwebview.


Here you can check if wifi has connection, 3g,or none atall:

if ([[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] == ReachableViaWiFi) {
    // Do something that requires wifi
} else if ([[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] == ReachableViaWWAN) {
    // Do something that doesnt require wifi
} else if ([[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] == NotReachable) {
    // Show alert because no wifi or 3g is available..
}

Apple provides all the necessary Reachability api/source here: Reachability Reference


I have made these custom convenience functions for myself in all my projects:

+ (BOOL)getConnectivity {
    return [[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] != NotReachable;
}

+ (BOOL)getConnectivityViaWiFiNetwork {
    return [[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] == ReachableViaWiFi;
}

+ (BOOL)getConnectivityViaCarrierDataNetwork {
    return [[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] == ReachableViaWWAN;
}

And used like so:

if ([ServerSupport getConnectivity]) {
    // do something that requires internet...
else {
    // display an alert
    UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Network Unavailable" 
                                                     message:@"App content may be limited without a network connection!" 
                                                    delegate:self 
                                           cancelButtonTitle:@"OK" 
                                           otherButtonTitles:nil] autorelease];
    [alert show];
}


If your application requires persistent internet connection, you can include the key UIRequiresPersistentWiFi in your plist file. Using this, iOS will automatically display an alert to the user in the airport mode.

Also, iOS will ensure that the wifi radio isn't switched off/hibernated while your app is in the foreground.


Note that internet connectivity is very dynamic on a mobile device (multiple radios, multiple access points, multiple cell towers, travel, competing radio interference, "your holding it wrong", etc.), and thus Reachability is not a perfect solution, as the net connectivity can very often can change just before, during or after Reachability performs its checks. Reachability can outright lie (yes we're connected to an access point, true even if broadband is dead on the other side of the access point). It's also quite common for Reachability to report no connectivity, just as its own request turns the radios on to get a great connection a couple seconds later.

Best thing to do is to just try to access data from the network, spin an activity indicator while waiting, and offer the user some API element to give up if they end up waiting too long, in their opinion, not some developer's opinion... They know better than you do how long it takes for a (re)connection to come up in their area, and how long they are willing to wait.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜