Search code examples
iosobjective-creachability

Can any one suggest sample for Reachability check when wifi loss inter net connection


I have added reachability into my project following things are working fine. 1.It Check successfully host request, wifi or mobile date active connection.. but i have tested the reachability of wifi with loss of internet connection , it may give the results like reachable via wifi...(example like you have active wifi connection but no internet received form wifi)

I did added NSTimers and achieved exact result, but i want to achieve the this thing by reachability so can anyone help to solve my issue...


Solution

  • Here is my answer ,

    I did not initialize timers just using completion blocks , but completion blocks with delay almost equal to NSTIMERS.

    I have created api for reachability with all conditions..

    just attach sample method to check the all conditions..

    -(void) blocksWithReachabiltyCheck :(bool) r_Status
    {
        __weak id weakSelf = self;
        callBack = ^{
    
            double delayInSeconds = 10.0;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    
                id strongSelf = weakSelf;
    
                if (!strongSelf) {
                    return;
                }
                // Schedule the timer again
                //callBack();
                [weakSelf targetMethod:r_Status];
            });
    
        };
    
        // Start the timer for the first time
        callBack();
    }
    
    -(void)targetMethod:(bool)sender
    {
        NSString *remoteHostName = @"www.apple.com";
        // NSString *remoteHostLabelFormatString = NSLocalizedString(@"Remote Host: %@", @"Remote host label format string");
        //    self.remoteHostLabel.text = [NSString stringWithFormat:remoteHostLabelFormatString, remoteHostName];
        // NSLog(@"%@",remoteHostLabelFormatString);
        self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
        [self.hostReachability startNotifier];
    
    
        self.internetReachability = [Reachability reachabilityForInternetConnection];
        [self.internetReachability startNotifier];
    
    
        self.wifiReachability = [Reachability reachabilityForLocalWiFi];
        [self.wifiReachability startNotifier];
    
        if (sender == YES)
        {
            callBack();
        }
    
    }
    
    
    
      //stop timer
        -(void) RXStopNotifier
        {
    
            [self blocksWithReachabiltyCheck:NO];
        }
        //start notifier with host name
        -(void) RXStartNotifier:(NSString *)hostNameString
        {
    
            [self blocksWithReachabiltyCheck:YES];
            hostName  = hostNameString;
        }
    
    @synthesize callBack;
    @property (copy)__block void (^callBack)(void) ;
    
    //Notification method
    - (void) RXSeachabilityChanged:(NSNotification *)note
    {
        if (timerFlag == false)
        {
            timerFlag = true;
            Reachability* curReach = [note object];
            NetworkStatus netStatus = [curReach currentReachabilityStatus];;
            statusReach = 0;
    
            switch (netStatus)
            {
                case NotReachable:        {
                    NSLog(@"Not Access ");
                    statusReach = 0;
                    break;
                }
    
                case ReachableViaWWAN:
                    //            {
                    //                NSLog(@"Reachable WWAN");
                    //                statusReach = 1;
                    //                //            imageView.image = [UIImage imageNamed:@"WWAN5.png"];
                    //                break;
                    //            }
                case ReachableViaWiFi:        {
                    if (instantFlag == NO)
                    {
    
                        NSLog(@"Reachable WIFI or Reachable WWAN");
                        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:hostName] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:1];
                        NSURLResponse *response = nil;
                        NSError *error = nil;
                        [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
                        //NSLog(@"response %d", [(NSHTTPURLResponse *)response statusCode]);
                        if ([(NSHTTPURLResponse *)response statusCode] == 200) {
    
                            statusReach = 1;
                            NSLog(@"Success");
                        }
                        else
                        {
    
    
                            statusReach = 2;
                            NSLog(@"Failed");
                        }
                    }
                    else
                    {
                        statusReach = 1;
                    }
                    break;
                }
            }
    
    
        }
    }
    

    if any one having doubts just reach me....