Search code examples
iosobjective-creachability

Detecting internet connectivity continually


I want my app to detect the internet connection loss automatically. So im using the following code.

- (void)applicationDidBecomeActive:(UIApplication *)application {

    Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
    if (networkStatus == NotReachable) {
        [Settings hideSpinner];
         //Show no internet connectivity dialog.
    } else {
    }
}

But the problem is that it is not checking the internet connectivity continually. it checks only when the app has become active. How can I be able to check for internet connection continually throughout the app life cycle and throw an warning if internet goes off automatically?


Solution

  • Add obeserver like this in Reachability method.

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

    It will call automatically when your app open/in background mode and it call reachabilityChanged.

    2) [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeInternetConnection" object:nil];

    ChangeInternetConnection you have to add observer to your ViewController to get status when internet changing it's status.

     - (void) checkInternetConnetion {
    
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    
            //NSString *remoteHostName = @"www.apple.com";
    
    
        self.internetReachability = [Reachability reachabilityForInternetConnection];
            [self.internetReachability startNotifier];
            [self updateInterfaceWithReachability:self.internetReachability];
        }
    
    
        #pragma mark - Reachability Methods
    
        - (void)updateInterfaceWithReachability:(Reachability *)reachability {
         if (reachability == self.internetReachability) {
                [self checkStatus:reachability];
            }
    
            if (reachability == self.wifiReachability) {
                [self checkStatus:reachability];
            }
        }
    
    
        -(void)checkStatus :(Reachability *)reachability {
    
            NetworkStatus netStatus = [reachability currentReachabilityStatus];
            BOOL connectionRequired = [reachability connectionRequired];
            NSString* statusString = @"";
    
            switch (netStatus) {
    
                case NotReachable: {
    
                   self.isInternetOn = FALSE;
    
                    break;
                }
    
                case ReachableViaWWAN: {
                    self.isInternetOn = TRUE;
    
                    break;
                }
                case ReachableViaWiFi: {
                    self.isInternetOn = TRUE;
    
                    break;
                }
            }
    
    
    
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeInternetConnection" object:nil];
    
          }
    
        - (void) reachabilityChanged:(NSNotification *)note {
    
            Reachability* curReach = [note object];
            NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
            [self updateInterfaceWithReachability:curReach];
        }