可达性在iOS中真正意味着什么?

时间:2013-07-02 21:28:32

标签: ios reachability

我在我的应用中有这个代码:

-(void)reachAlert:(Reachability*)currentReach {

    if(currentReach == hostReach) {
        //Make sure we have internet connectivity
        //UIAlertView *internetAlert = [[UIAlertView alloc] initWithTitle:@"Excellent" message:@"Host Reached" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
        //[internetAlert show];
        [[SDSyncEngine sharedEngine] startSync];


    }
    /**
     if(currentReach == internetReach) {
     //Make sure we have internet connectivity
     UIAlertView *internetAlert = [[UIAlertView alloc] initWithTitle:@"Good"
     message:@"Internet"
     delegate:self
     cancelButtonTitle:@"Cancel"
     otherButtonTitles:@"Ok", nil];
     [internetAlert show];
     }
     **/
    if(currentReach == wifiReach) {
        //Make sure we have internet connectivity
        UIAlertView *internetAlert = [[UIAlertView alloc] initWithTitle:@"Bad News"
                                                                message:@"Only wifi"
                                                               delegate:self
                                                      cancelButtonTitle:@"Cancel"
                                                      otherButtonTitles:@"Ok", nil];
        [internetAlert show];
    }
    [TestFlight passCheckpoint:@"reachAlert"];


}

正如你所看到的,我评论了internetReach,因为我认为,更重要的是我们有hostReach。因此,默认情况下我们必须有互联网覆盖我还注释掉了hostReach警报,因为我只想在没有互联网连接的情况下提醒用户。

然而,当在wifi上测试应用程序时,我得到的坏消息只有wifi消息。为什么它不提供hostReach警报?

2 个答案:

答案 0 :(得分:1)

可达性对于显示错误消息并不理想。理想情况下,当您尝试使用的连接失败时,您应该显示错误消息,例如NSURLConnection返回-1009错误。

答案 1 :(得分:0)

不知道它是否有帮助,但我发现它在某个地方,并认为这是一个更好的解决方案 - 尽管有人可能会争论它:

- (void) verifyInternetConnection
{
    NSURL *scriptUrl = [NSURL URLWithString:@"http://youtube.com"];
    NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
    if (!data)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Internet Required" message:@"This device is not currently connected to the Internet" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        alert.tag = 1;
        [alert show];
    }
}