检查 iPhone 是否有活跃的互联网连接情况

发布于 2024-12-07 19:54:57 字数 744 浏览 5 评论 0原文

我想检查用户是否有有效的互联网连接。我就是这样实现的。它似乎工作正常,但问题是它总是显示我的 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

 }

I would like to check to see if the user has an active internet connection. This is how I have implemented it. It seems to work fine but the problem is it ALWAYS shows that there is no connection on my iPhone simulator (uialert comes up) even when my wifi is turned on or off. Does any know what I am doing wrong?
Thanks for your help!

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

 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情稳全场 2024-12-14 19:54:57

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

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 消息(本地化)。

This is how I have done it in my apps:

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

What is does is that it checks for an internetconnection, not if it can reach a domain. If no internet connection (wifi or celluar) it will show an UIAlertView message (localized).

明天过后 2024-12-14 19:54:57

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

Don't check. The radio often starts turning on and establishing a connection just after reachability reports no network (the reachability reporting that there is no network may be what starts this process and thus renders it's own answer to be false a few seconds later.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文