首页 文章

在Iphone模拟器上使用可达性,没有wifi

提问于
浏览
2

我在我的IOS应用程序中使用Reachability来确定连接 .

以下帖子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];

}

//状态发生变化时,由Reachability调用 . - (void)reachabilityChanged:(NSNotification *)note {Reachability * curReach =(Reachability *)[note object]; NSParameterAssert([curReach isKindOfClass:[Reachability class]]); NSLog(@“Reachability changed:%@”,curReach); networkStatus = [curReach currentReachabilityStatus];

}

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

有谁知道如何处理这种连接情况更好的错误?
enter image description here

1 回答

  • 2

    create following method in appdelegate to use it on any class.

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

相关问题