Monday, March 10, 2014

Check network connectivity

Note : Import Reachability files into your project

-(void)updateWifiAvailability; // Declare in Interface

 [self updateWifiAvailability]; // Invoke function

   /*Configure and check for reachability */
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(wifiChanged:) name: kReachabilityChangedNotification object: nil];

    self.wifiReachability = [Reachability reachabilityForLocalWiFi];
[self.wifiReachability startNotifier];

#pragma mark -
#pragma mark Internal Methods
-(void)updateWifiAvailability
{
    NetworkStatus netStatus = [self.wifiReachability currentReachabilityStatus];
    if (netStatus == ReachableViaWiFi){
         // Network available
        _hasWifiConnection = YES;
    }
    else {
        // Network not available
        _hasWifiConnection = NO;
    }
}

-(void)wifiChanged:(NSNotification *)n
{
    [self updateWifiAvailability];
}