使用 ios5 在 iPad 应用程序上测试互联网连接
我一直在论坛上搜索如何检查我的 ipad 应用程序中是否有互联网。我刚刚使用其他视图控制器创建了一个简单的 webview 项目,我需要在互联网不可用时显示 UIAlert 消息。就我而言,当我运行应用程序时它会显示消息。当我通过互联网运行应用程序然后停用互联网时,它不会显示 UIAlert 消息,也就是说,如果我在视图之间切换,它不会再显示没有互联网连接。
我在我的项目中遵循了这种实现方式:(抱歉我的错误,这是我遵循的链接)http://mozymac.com/forums/f54/how-check-if-there-internet-connection-iphone-os-devices-595/ [这是新编辑的问题]
除此之外我在 Stackoverflow 论坛中浏览了一些之前的问题,例如:如何在 iOS 或 OSX 上检查活动的互联网连接?
但每个人都有自己的版本。如果有人对 ios5 有更新的方法,那么 xcode 4.2.1 将会对我有所帮助。
谢谢
I have been searching through the forum regarding how to check whether there is internet or not in my ipad app. I just created a simple webview project with other view controllers and I need to display a UIAlert message when the internet is not available. In my case it is displaying the message when I run the app. When I run the app with internet and then deactivate the internet, it does not show the UIAlert message, that is if I switch between the views, it does not any more show the no internet connection.
I have followed this way of implementation in my project: (sorry my mistake this is the link I followed) http://mozymac.com/forums/f54/how-check-if-there-internet-connection-iphone-os-devices-595/ [This is the new edited question]
Apart from that I went through some of the previous questions in Stackoverflow forum like for ex: How to check for an active Internet connection on iOS or OSX?
But everybody has their own version. If any one has a much more updated method for ios5, xcode 4.2.1 of how to accomplish this then would be helpful for me.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在实际尝试在
UIWebView
中加载请求之前,您是否想要检查互联网连接?最佳实践是开始加载,然后使用
UIWebViewDelegate
/NURLConnectionDelegate
检查 NSError 以查看问题所在。如果出现网络故障,您将收到一个错误,其域等于NSURLErrorDomain
。错误代码将指示问题所在,请参阅NSError 代码
枚举。并且只有在第一个错误发生后,您才能启动可达性,以查看互联网连接何时再次可用。或者更简单,只需让用户重试即可。
使用
Reachability
代码实际上会产生一些开销。检查互联网是否可用需要时间,您也可以用它来设置实际连接。示例
由于您使用的是
UIWebView
,因此您应该实现以下委托方法以收到错误通知。在这个委托方法中,您应该做任何需要的事情来实现您想要的。您可以自己重试请求,向用户显示消息或使用 Apple 提供的 Reachability 示例中的代码开始监听可达性更改。
Is there a reason why you want to check for internet connection before actually trying to load a request in the
UIWebView
?Best practice is to just start loading, and use your
UIWebViewDelegate
/NURLConnectionDelegate
to inspect the NSError to see what is wrong. In case of network failure you will receive an error with a domain equal toNSURLErrorDomain
. The error code will indicate what the problem is, see theNSError codes
enum.And only after the first error start your reachability to see when the internet connection becomes available again. Or easier, just let the user retry.
Using the
Reachability
code will actually cause some overhead. It takes time to check if the internet is available, which you could just have used to set up the actual connection as well.Example
Since you are using a
UIWebView
you should implement the following delegate method to be notified of errors.In this delegate method you should do whatever is needed to achieve what you want. You could retry the request yourself, show a message to the user or start listening for reachability changes using the code from the Reachability example provided by Apple.
苹果有一个,叫做可达性(Reachability)。这是它的链接。
http://developer.apple.com/library/ ios/ipad/#samplecode/Reachability/Introduction/Intro.html
Apple has one, it's called Reachability. Here's the link to it.
http://developer.apple.com/library/ios/ipad/#samplecode/Reachability/Introduction/Intro.html
检查互联网连接的最佳方法是“可达性”应用程序
链接
不然的话
Best way to check internet connection is Reachibility application
link
Or else