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

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我在应用程序中的做法:
它的作用是检查互联网连接,而不是检查是否可以到达域。如果没有互联网连接(wifi 或蜂窝网络),它将显示 UIAlertView 消息(本地化)。
This is how I have done it in my apps:
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).
别检查。无线电通常在可达性报告没有网络后立即开始打开并建立连接(可达性报告没有网络可能是启动此过程的原因,因此在几秒钟后使其自己的答案为假。)
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.)