简体   繁体   English

可达性在模拟器上起作用,但在设备上不起作用

[英]Reachability working on simulator but not on device

In my project I am using Reachability class provided by Apple. 在我的项目中,我正在使用Apple提供的Reachability类。 When there is no internet connection, I am displaying an alert message. 没有互联网连接时,我会显示一条警报消息。 Everything is working fine, when I test it on the simulator, but when am running it on iPad, the alert message is not shown when there is no internet. 当我在模拟器上进行测试时,一切正常,但是在iPad上运行时,如果没有互联网,则不会显示警报消息。

I am running the code on iOS 5.0. 我在iOS 5.0上运行代码。

Any help would be appreciated. 任何帮助,将不胜感激。

EDIT: 编辑:

-(BOOL)isInternetConnectionPresent{

Reachability *objReachability = [Reachability reachabilityForInternetConnection];    
NetworkStatus internetStatus = [objReachability currentReachabilityStatus];

if(internetStatus != NotReachable)
{
    return YES;
}


   return NO;
}

UPDATE: 更新:

Used NSLog to debug. 使用NSLog进行调试。 Seems there was some problem with WWAN, even when there was no SIM card. 即使没有SIM卡,WWAN似乎也有问题。 Restarted the iPad & switched OFF & ON the Wi Fi again. 重新启动iPad,然后再次关闭和打开Wi Fi。 Now it works fine. 现在工作正常。 Thanks for the help guys. 感谢您的帮助。

You have to check all the NetworkStatus and Cross Check the device Wifi Connection status again 您必须检查所有NetworkStatus并再次交叉检查设备的Wifi连接状态

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;

}

In My case the following code snippet is working perfectly with iOS 5. Here i am checking internet connectivity with WIFI. 就我而言,以下代码段可在iOS 5上正常运行。在这里,我正在检查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.

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM