【问题标题】:Reachability working on simulator but not on device可达性在模拟器上工作但在设备上不工作
【发布时间】:2013-07-30 22:17:13
【问题描述】:

在我的项目中,我使用的是 Apple 提供的 Reachability 类。当没有互联网连接时,我会显示一条警报消息。一切正常,当我在模拟器上测试它时,但在 iPad 上运行它时,没有互联网时不会显示警报消息。

我在 iOS 5.0 上运行代码。

任何帮助将不胜感激。

编辑:

-(BOOL)isInternetConnectionPresent{

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

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


   return NO;
}

更新:

使用 NSLog 进行调试。即使没有 SIM 卡,WWAN 似乎也有问题。重新启动 iPad 并再次关闭和打开 Wi Fi。现在它工作正常。谢谢大家的帮助。

【问题讨论】:

  • 如果您添加更多细节,您可能会得到更好的答案。例如,您可以发布您的实现代码。也尝试做一些调试:至少有一些NSLogs。 Reachability 当然也应该在设备上工作。我用它很多应用程序。
  • 正如我所说,它在模拟器中运行良好!!...我已经编辑了我的问题以添加代码。
  • 你可能只需要清理项目,从设备中删除应用程序......无论如何:你最好使用观察者 - 可以找到最好的例子之一here

标签: iphone ios ipad reachability


【解决方案1】:

您必须检查所有NetworkStatus并再次交叉检查设备Wifi连接状态

示例:

// 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;

}

【讨论】:

  • 我刚刚添加了我的代码来检查互联网。它应该做同样的事情。
  • 应用我的答案并检查 NSlogs。它应该说出确切的原因,为什么会有连接
【解决方案2】:

在我的情况下,以下代码 sn-p 与 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;

}

------------ 现在可以检查以下代码行。

 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.

}

【讨论】:

    猜你喜欢
    • 2012-08-19
    • 2023-03-23
    • 2013-07-02
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多