Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?


2 Answers

Once your application has launched, you can fire a NSTimer to do the same:

- (void)applicationDidBecomeActive:(UIApplication *)application {
   NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                              target:self
                                            selector:@selector(checkForConnectivity)
                                            userInfo:nil
                                             repeats:YES];
}

-(void)checkForConnectivity {
       Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
       NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
       if (networkStatus == NotReachable) {
              //No internet connectivity - perform required action
       }
       else {
              //Internet connectivity is valid
      }
}

Thanks!

like image 70
Kunal Shah Avatar answered May 26 '26 12:05

Kunal Shah


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];
    }
like image 42
Bhoomi Jagani Avatar answered May 26 '26 10:05

Bhoomi Jagani