5

我想检查用户是否有活动的互联网连接。这就是我实现它的方式。它似乎工作正常,但问题是它总是显示我的 iPhone 模拟器上没有连接(uialert 出现),即使我的 wifi 打开或关闭。有谁知道我做错了什么?谢谢你的帮助!

Reachability *r= [Reachability reachabilityWithHostName:@"http://www.google.com"];
    NetworkStatus internetStatus= [r currentReachabilityStatus];

 if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
    {

        UIAlertView *alert= [[UIAlertView alloc] initWithTitle:@"No internet" message:@"No internet connection found. Please try again later"
                                                      delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }

 else {

 // execute code in app

 }
4

2 回答 2

6

这就是我在我的应用程序中所做的:

Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];

if(internetStatus == NotReachable) {
    UIAlertView *errorView;

    errorView = [[UIAlertView alloc]
                 initWithTitle: NSLocalizedString(@"Network error", @"Network error")
                 message: NSLocalizedString(@"No internet connection found, this application requires an internet connection to gather the data required.", @"Network error")
                 delegate: self
                 cancelButtonTitle: NSLocalizedString(@"Close", @"Network error") otherButtonTitles: nil];

    [errorView show];
    [errorView autorelease];
}

它所做的是检查互联网连接,而不是它是否可以到达域。如果没有互联网连接(wifi 或蜂窝),它将显示 UIAlertView 消息(本地化)。

于 2011-10-02T21:02:11.467 回答
0

不要检查。无线电通常在可达性报告没有网络后立即开始打开并建立连接(可达性报告没有网络可能是启动此过程的原因,因此几秒钟后它自己的答案是错误的。)

于 2011-10-03T04:49:07.683 回答