xcode iPhone 应用程序,在用户未连接到互联网时提醒用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7562119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone app, alert users when they aren't connected to the internet?
提问by davis
What's an easy way to code an alert that warns users when they aren't connected to the Internet? I'm using Xcode, right now when there is no connection it is just a white screen in uiwebview.
编写警报以在用户未连接到 Internet 时发出警告的简单方法是什么?我正在使用 Xcode,现在当没有连接时,它只是 uiwebview 中的一个白屏。
回答by chown
Here you can check if wifi has connection, 3g,or none atall:
在这里您可以检查 wifi 是否有连接、3g 或根本没有连接:
if ([[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] == ReachableViaWiFi) {
// Do something that requires wifi
} else if ([[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] == ReachableViaWWAN) {
// Do something that doesnt require wifi
} else if ([[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] == NotReachable) {
// Show alert because no wifi or 3g is available..
}
Apple provides all the necessary Reachability api/source here: Reachability Reference
Apple 在此处提供了所有必要的 Reachability api/源:Reachability Reference
I have made these custom convenience functions for myself in all my projects:
我在我所有的项目中为自己制作了这些自定义的便利功能:
+ (BOOL)getConnectivity {
return [[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] != NotReachable;
}
+ (BOOL)getConnectivityViaWiFiNetwork {
return [[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] == ReachableViaWiFi;
}
+ (BOOL)getConnectivityViaCarrierDataNetwork {
return [[Reachability reachabilityWithHostName:@"google.com"] currentReachabilityStatus] == ReachableViaWWAN;
}
And used like so:
并像这样使用:
if ([ServerSupport getConnectivity]) {
// do something that requires internet...
else {
// display an alert
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Network Unavailable"
message:@"App content may be limited without a network connection!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil] autorelease];
[alert show];
}
回答by Akshay
If your application requires persistent internet connection, you can include the key UIRequiresPersistentWiFi
in your plist file. Using this, iOS will automatically display an alert to the user in the airport mode.
如果您的应用程序需要持久的 Internet 连接,您可以UIRequiresPersistentWiFi
在 plist 文件中包含密钥。使用此功能,iOS 将在机场模式下自动向用户显示警报。
Also, iOS will ensure that the wifi radio isn't switched off/hibernated while your app is in the foreground.
此外,当您的应用程序处于前台时,iOS 将确保 wifi 无线电不会关闭/休眠。
回答by hotpaw2
Note that internet connectivity is very dynamic on a mobile device (multiple radios, multiple access points, multiple cell towers, travel, competing radio interference, "your holding it wrong", etc.), and thus Reachability is not a perfect solution, as the net connectivity can very often can change just before, during or after Reachability performs its checks. Reachability can outright lie (yes we're connected to an access point, true even if broadband is dead on the other side of the access point). It's also quite common for Reachability to report no connectivity, just as its own request turns the radios on to get a great connection a couple seconds later.
请注意,移动设备上的互联网连接是非常动态的(多个无线电、多个接入点、多个蜂窝塔、旅行、相互竞争的无线电干扰、“你错了”等),因此可达性不是一个完美的解决方案,因为网络连接经常会在 Reachability 执行检查之前、之中或之后发生变化。可达性可能是彻头彻尾的谎言(是的,我们已连接到一个接入点,即使接入点另一侧的宽带已死,也是如此)。Reachability 报告没有连接也很常见,就像它自己的请求在几秒钟后打开无线电以获得良好的连接一样。
Best thing to do is to just try to access data from the network, spin an activity indicator while waiting, and offer the user some API element to give up if they end up waiting too long, in theiropinion, not some developer's opinion... They know better than you do how long it takes for a (re)connection to come up in their area, and how long they are willing to wait.
最好的办法是尝试从网络访问数据,在等待时旋转活动指示器,并为用户提供一些 API 元素,如果他们最终等待的时间过长,他们认为,而不是某些开发人员的意见。 . 他们比你更清楚在他们的地区出现(重新)连接需要多长时间,以及他们愿意等待多长时间。