2013-07-02 47 views
0

我在我的应用程序的代码:Reachability在iOS中的真正含义是什么?

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

回答

1

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

+0

好吧,我知道了......所以基本上只是在连接失败的情况下弹出alertview,对吧? – marciokoko

+0

是的,错误1009意味着你无法连接。你当然可以得到其他的错误,所以你应该采取相应的行动。 –

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]; 
    } 
} 
+0

这是一个好简单的方法,但我会指出(也许是非常明显),你不应该使用具有大型内容的URL,如YouTube。相反,请制作一个网址,以最少的已知数据作为回应。 – Hippocrates

+0

@Hippocrates ...好的提示! :) – marciokoko

+0

@RegularExpression .... thx给你呢! – marciokoko

相关问题