Internet connection (Rechability) not work

| | August 7, 2015

I use below code for internet connection but not work(reachabilityChanged does’t call when wifi or internet change)

//Internet.m : 
...
@interface Internet ()
@property (nonatomic) Reachability *Wifi;
@end

@implementation Internet

-(void)checkNetwork
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    self.Wifi = [Reachability reachabilityForLocalWiFi];
    [self.Wifi startNotifier];
}

- (void) reachabilityChanged:(NSNotification *)note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    if (curReach == self.Wifi)
    {
        NetworkStatus netStatus = [curReach currentReachabilityStatus];
        if(netStatus == NotReachable)
        {
            NSString *status=@"NO";
        }//if
        else if(netStatus == ReachableViaWiFi)
        {
            NSString *status=@"Yes";
        }//else
    }//if
}//reachabilityChanged

//ViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];

    [self performSelectorInBackground:@selector(callInternet) withObject:nil];
}//load

-(void)callInternet
{
    inter=[[Internet alloc]init];//inter is instance variable
    [inter checkNetwork];
}//callInternet

One Response to “Internet connection (Rechability) not work”

  1. @sajidZare,

    in Appdelegate.h file

    //Check Internet Connection
    .//Define method for that
    -(BOOL)checkInternetConnection;
    

    in Appdelegate.m file

     //Check Internet Plugin
        #include<unistd.h>
        #include<netdb.h>
    
        #pragma mark Check Internet connection
        -(BOOL)checkInternetConnection {
    
            char *hostname;
            struct hostent *hostinfo;
            hostname = "google.com";
            hostinfo = gethostbyname (hostname);
            if (hostinfo == NULL)
            {
                NSLog(@"-> no connection!n");
                return NO;
            }
            else{
                NSLog(@"-> connection established!n");
                return YES;
            }
    
        }
    

    //This is best solution, try it, may be help to you.

    Another Solution
    in your.h File

        #import "Reachability.h"            // internet Rechability
        //---rechability methods---//
        @property (nonatomic) Reachability *internetReachability;
    -(void)checkInternet;
    

    in your.m File

    -(void)checkInternet
    {
        self.internetReachability = [Reachability reachabilityForInternetConnection];
        if (self.internetReachability.currentReachabilityStatus==NotReachable)
        {
            NSLog(@"internet is not available..");
            [self internetAlertView];
        }
    
    
        else
            {
                NSLog(@"internet Available..");
    *//Here you can do whatever you want to do//*
                [self BTN_Spark:self];
            }
            [self.internetReachability startNotifier];
        }
    
    
        -(void)internetAlertView
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Please check the internet connection!!!" delegate:self cancelButtonTitle:@"OK"  otherButtonTitles:nil];
            [alertView show];
        }
    

Leave a Reply