0

我的应用程序中有此代码:

-(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 警报?

4

2 回答 2

1

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

于 2013-07-02T23:55:50.917 回答
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];
    }
}
于 2013-07-02T21:32:45.923 回答