Sandhya Sandhya - 2 days ago 4
iOS Question

Automatically detect if my mobile is connected to Internet

-(BOOL)IsConnectionAvailable{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}


I am using above code and have to call that periodically to check if there is network connection.
I am working on chat application where i want to send messages immediately one the device get connected to network.

Thanks

Answer

you can use kReachabilityChangedNotification

   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForReachability:) name:kReachabilityChangedNotification object:nil];

    } 

 - (void) checkForReachability:(NSNotification *)notification
        {
             if([self IsConnectionAvailable])// use your method here
              {
                  // net available
               }
             else
              {
                  // no internet 
              }

  }
Comments