ios iPhone SDK 互联网连接检测
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/596589/
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 SDK Internet connection detection
提问by givp
I'm working on an iPhoneapplication that makes a few calls to web services. I posted this application on the Apple store but it got rejected (and rightly so) since there was no error message displayed to the user if no Internet connection is available. Since obviously the application would not work without it.
我正在开发一个对网络服务进行几次调用的iPhone应用程序。我在 Apple Store 上发布了这个应用程序,但它被拒绝了(这是正确的),因为如果没有可用的 Internet 连接,则不会向用户显示错误消息。因为显然没有它,应用程序将无法工作。
So I just wanted to know how to best achieve this? I'm guessing something needs to go in the viewDidLoad
method that will throw an alert box saying something like "You need an Internet connection to use this application".
所以我只是想知道如何最好地实现这一目标?我猜该viewDidLoad
方法需要添加一些内容,该方法会抛出一个警告框,上面写着“您需要 Internet 连接才能使用此应用程序”。
Any ideas would be appreciated.
任何想法,将不胜感激。
回答by Andrew Grant
If your application must have network access the easiest way is to add the following settings to your info.plist as boolean values.
如果您的应用程序必须具有网络访问权限,最简单的方法是将以下设置作为布尔值添加到您的 info.plist 中。
SBUsesNetwork - Ensure the device has an active connection(Edit: not applicable, this seems to be a private API someone found at some point. It is not in Apple's developer documentation.)- UIRequiresPersistentWiFi - Ensures the device is connected via WiFi
SBUsesNetwork - 确保设备具有活动连接(编辑:不适用,这似乎是某人在某个时候发现的私有 API。它不在 Apple 的开发人员文档中。)- UIRequiresPersistentWiFi - 确保设备通过 WiFi 连接
If your choice is not true then the user will be presented with an appropriate message when starting your application. Best of all this message is from the OS and thus is localized.
如果您的选择不正确,则在启动您的应用程序时将向用户显示一条适当的消息。最重要的是,此消息来自操作系统,因此是本地化的。
If your application cannot download data from a website while running (loss of signal, site down) you should still warn the user though and not just spin indefinitely.
如果您的应用程序在运行时无法从网站下载数据(信号丢失,站点关闭),您仍然应该警告用户,而不仅仅是无限期地旋转。
回答by Alex Reynolds
Apple Developer Connection has a sample application (Reachability) that uses the System Configuration framework to determine network status. It will tell you whether you have a WiFi, EDGE/3G or no Internet connection.
Apple Developer Connection 有一个示例应用程序 ( Reachability),它使用系统配置框架来确定网络状态。它会告诉您是否有 WiFi、EDGE/3G 或没有互联网连接。
You would use portions of this code in your application to determine network state, and then provide interface cues if no connection is available, such as a UIAlertView.
您将在应用程序中使用此代码的一部分来确定网络状态,然后在没有可用连接时提供界面提示,例如UIAlertView。
回答by occulus
Cautionary word: beware SBUsesNetwork. I would personally love to know where SBUsesNetwork originally came from, because it's not mentioned anywhere in Apple's docs that I can find. When I add the key to my app's plist (as a boolean) and set to true, it doesn't seem to affect the behaviour of my app -- I get no warning about airplane mode, whether starting app completely afresh, or foregrounding a previous launch that was backgrounded.
警告词:当心 SBUsesNetwork。我个人很想知道 SBUsesNetwork 最初来自哪里,因为我可以找到的 Apple 文档中没有提到它。当我将密钥添加到我的应用程序的 plist(作为布尔值)并设置为 true 时,它似乎不会影响我的应用程序的行为——我没有收到关于飞行模式的警告,无论是完全重新启动应用程序,还是将应用程序置于前台之前发布的背景。
My app has UIRequiresPersistentWifi set to true, which appears to also do the job people claim SBUsesNetwork does (plus other things!).
我的应用程序将 UIRequiresPersistentWifi 设置为 true,这似乎也完成了人们声称 SBUsesNetwork 所做的工作(以及其他事情!)。
(I'm running iOS4.2.1 on an iPhone 4, XCode 3.2.5 64 bit).
(我在 iPhone 4、XCode 3.2.5 64 位上运行 iOS4.2.1)。