IPhone, warn users when they are not connected to the Internet?

What is an easy way to encode a warning warning users when they are not connected to the Internet? I am using Xcode, right now when there is no connection, it is just a white screen in uiwebview.

+1
source share
3 answers

Here you can check if there is a wifi, 3g or none atall connection:

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 api / source features: Reachability Reference


I made these custom convenience features for myself in all of 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 this:

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];
}
+6
source

, UIRequiresPersistentWiFi plist. , iOS .

, iOS , Wi-Fi- / , .

+3

, ( , , , , , " " ..), , , , , , Reachability . (, , , ). , Reachability , , .

It’s best to just try to access data from the network, turn off the activity indicator while waiting, and offer the user an API element to refuse if they end up waiting too long, in their strong opinion, and not the opinion of the developers ... They know better than you do how long it takes to connect (re) to their appearance and how much time they are willing to wait.

0
source

All Articles