Search code examples
ioscocoa-touchreachability

Internet reachability issue


I am using MKNetworkKit and using singelton class for internet availablity check. The way i am using class is:

+ (KCShared *)instance {
    static KCShared *object = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        object = [[self alloc] init];
    });
    return object;
}

- (id) init {

    self = [super init];
    if (self != nil) {
        internetReachable = [Reachability reachabilityForInternetConnection];
        [internetReachable startNotifier];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
        hostReachable = [Reachability reachabilityWithHostname: @"www.apple.com"];
        [hostReachable startNotifier];
    }
    return self;
}
- (void) checkNetworkStatus:(NSNotification *)notice
{
    internetStatus = [internetReachable currentReachabilityStatus];
    if(internetStatus == NotReachable){
        NSLog(@"Internet not available");
    }
    else    {
         NSLog(@"Internet available");
    }
}

And in my appDelegate i have added code like:

[KCShared instance];

On first run method -checkNetworkStatus calls. but when i tried to turn off my wifi it doesn't calls.

i don't know why its happened..


Solution

  • try this for init

    - (id) init {
        self = [super init];
        if (self != nil) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
            internetReachable = [Reachability reachabilityForInternetConnection];
            [internetReachable startNotifier];
            hostReachable = [Reachability reachabilityWithHostname: @"www.apple.com"];
            [hostReachable startNotifier];
             }
            return self;
       }
    

    `