【问题标题】:Using reachability on Iphone simulator and no wifi在 Iphone 模拟器上使用可达性并且没有 wifi
【发布时间】:2012-08-30 00:33:33
【问题描述】:

我在我的 IOS 应用程序中使用可达性来确定连接。

关注这篇帖子wifi on iphone simulator

如果 wifi 关闭,模拟器的互联网连接不可用,但手机仍连接到 Wifi,因此连接没有改变。这一切都很好并且可以理解,当然我可以在设备本身上进行测试。

但是,我希望处理用户连接到 wifi 但 wifi 没有互联网连接的错误,如下面的屏幕截图。

我通过以下方式使用可达性:

#pragma mark - ()
- (void)monitorReachability {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:ReachabilityChangedNotification object:nil];

self.hostReach = [Reachability reachabilityWithHostname: @"api.parse.com"];
[self.hostReach startNotifier];

self.internetReach = [Reachability reachabilityForInternetConnection];
[self.internetReach startNotifier];

self.wifiReach = [Reachability reachabilityForLocalWiFi];
[self.wifiReach startNotifier];

}

//当状态改变时由可达性调用。 - (void)reachabilityChanged:(NSNotification* )note { 可达性 *curReach = (可达性 *)[注意对象]; NSParameterAssert([curReach isKindOfClass: [Reachability class]]); NSLog(@"可达性改变:%@", curReach); networkStatus = [curReach currentReachabilityStatus];

}

我在这里使用来自 tonymillion github 的 Reachability for ARC:https://github.com/tonymillion/Reachability

有谁知道我可以如何处理这种连接情况并出现更好的错误?

【问题讨论】:

    标签: ios ios5 ios-simulator reachability


    【解决方案1】:

    在 appdelegate 中创建以下方法以在任何类上使用它。

    -(BOOL)isHostAvailable
    {
        //return NO; // force for offline testing
        Reachability *hostReach = [Reachability reachabilityForInternetConnection];
        NetworkStatus netStatus = [hostReach currentReachabilityStatus];
        return !(netStatus == NotReachable);
    }
    

    【讨论】:

    • 谢谢,我在执行上述操作时仍然遇到同样的错误。错误 20606:1b03] 错误:错误域=NSURLErrorDomain 代码=-1009“Internet 连接似乎处于脱机状态。” UserInfo=0xc3748e0 {NSErrorFailingURLStringKey=api.parse.com/2/user_login, NSErrorFailingURLKey=api.parse.com/2/user_login, NSLocalizedDescription=Internet 连接似乎已脱机。, NSUnderlyingError=0xe0962c0 "Internet 连接似乎已脱机。"}(代码:100,版本:1.1。 0)
    猜你喜欢
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 2010-12-19
    • 2020-01-22
    • 2011-12-14
    • 1970-01-01
    相关资源
    最近更新 更多