1

Possible Duplicate:
How to check for an active Internet Connection on iPhone SDK?

What is the simplest way to implement Reachability (Code that notifies the user that there is no Internet connection) into an iOS App?

Community
  • 1
  • 1
Peter V
  • 2,478
  • 6
  • 36
  • 54
  • See: http://stackoverflow.com/questions/1083701/how-to-check-for-an-active-internet-connection-on-iphone-sdk – Ry- Aug 25 '11 at 23:00

1 Answers1

5

I did this in my appDelegate

in appDelegate.h:

@interface myAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet noInternetViewController *NoInternetViewController;
BOOL isShowingNoInternetScreen;
}

in appDelegate.m

//at top
#import "Reachability.h"

//somewhere under @implementation
-(void)checkInternetConnection {
Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];

NetworkStatus internetStatus = [r currentReachabilityStatus];

if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
    if (!isShowingNoInternetConnectionScreen) {
        [self.navigationController pushViewController:NoInternetViewController animated:YES];
        isShowingNoInternetConnectionScreen = YES;
    }

}
else if (isShowingNoInternetConnectionScreen) {
    [self.navigationController popViewControllerAnimated:YES];
    isShowingNoInternetConnectionScreen = NO;
    }
}

//inside application:didFinishLaunchingWithOptions: or in application:didFinishLaunching
isShowingNoInternetConnectionScreen = NO;

[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(checkInternetConnection) userInfo:nil repeats:YES];

I'm using a navigation controller, but if you aren't, then just change the pushViewController:animated: to presentModalViewController:animated: and change popViewControllerAnimated: to dismissModalViewController

You'll obviously need a view controller set up in Interface Builder that is tied to the IBOutlet as well. Just make this whatever you want the user to see when they have no connection.

Lance
  • 8,872
  • 2
  • 36
  • 47