Reachability working on simulator but not on device

后端 未结 2 1390
鱼传尺愫
鱼传尺愫 2021-01-16 05:34

In my project I am using Reachability class provided by Apple. When there is no internet connection, I am displaying an alert message. Everything is working fine, when I tes

相关标签:
2条回答
  • 2021-01-16 06:16

    In My case the following code snippet is working perfectly with iOS 5. Here i am checking internet connectivity with WIFI.

    - (NSString *)stringFromStatus:(NetworkStatus ) status {
    NSString *string;
    switch(status) {
        case NotReachable:
            string = @"Not Reachable";
            break;
        case ReachableViaWiFi:
            string = @"Reachable via WiFi";
            break;
        case ReachableViaWWAN:
            string = @"Reachable via WWAN";
            break;
        default:
            string = @"Unknown";
            break;
    }
    return string;
    

    }

    ------------------------ now with following line of code you can check.

     Reachability *reach =[Reachability reachabilityForLocalWiFi] ;
    NetworkStatus status = [reach currentReachabilityStatus];
    NSLog(@"%@", [self stringFromStatus: status]);
    
    if ([[self stringFromStatus: status] isEqualToString: @"Not Reachable"]) 
    {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Connection Failure !"
                              message:@"Your Device is not Connected to any active WIFI Connection."
                              delegate:self
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
    }
    else
    { //connected to internet.
    

    }

    0 讨论(0)
  • 2021-01-16 06:19

    You have to check all the NetworkStatus and Cross Check the device Wifi Connection status again

    Example:

    // to check if, wifi connected properly in current device.
    - (BOOL)networkCheck {
    
        Reachability *wifiReach = [Reachability reachabilityForInternetConnection];
        NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
    
        switch (netStatus)
        {
            case NotReachable:
            {
                NSLog(@"NETWORKCHECK: Not Connected");          
                return NO;
                break;
            }
            case ReachableViaWWAN:
            {
                NSLog(@"NETWORKCHECK: Connected Via WWAN");
                return NO;
                break;
            }
            case ReachableViaWiFi:
            {
                NSLog(@"NETWORKCHECK: Connected Via WiFi");
                return YES;
                break;
            } 
        }
        return false;
    
    }
    
    0 讨论(0)
提交回复
热议问题