0

In my iOS app, I'm trying to monitor Internet connectivity. I display an alert if the user is not connected to the Internet. However, I'm finding that the alert can take a very long time to be visible, on the order of 30 seconds. Is there some other way to more quickly display an alert if a user is not connected?

I'm testing with my iPhone 5S on airplane mode.

- (void)viewDidLoad
{
    [super viewDidLoad];
    Reachability *internetConnection = [Reachability reachabilityWithHostname:@"www.google.com"];

    // Internet is not reachable
    internetConnection.unreachableBlock = ^(Reachability*reach)
    {
        disconnected = YES;
        NSLog(@"no network connection");

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection"
                                                        message:@"You must be connected to the internet to use this app."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    };
scientiffic
  • 8,169
  • 13
  • 68
  • 138
  • 2
    do not check connection using hostname. Use SystemConfiguration framework instead. Look at Andrews answer which is bit down in this question - http://stackoverflow.com/questions/1083701/how-to-check-for-an-active-internet-connection-on-iphone-sdk – Sam B Sep 07 '14 at 20:42
  • thanks for the suggestion - this worked for me! – scientiffic Sep 07 '14 at 20:46

1 Answers1

0

Try this code :

-(void)setUpRechability
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil];
    }

- (void) handleNetworkChange:(NSNotification *)notice
{
    NetworkStatus remoteHostStatus = [self.reachability currentReachabilityStatus];

    if (!(remoteHostStatus == NotReachable)) {
         NSLog(@"Has internet");
    }
    else
    {
         NSLog(@"NO internet");
    }
}
Mostafa Abdellateef
  • 1,015
  • 14
  • 11