使用ios5在iPad应用上测试互联网连接 [英] Testing internet connection on iPad app using ios5

查看:181
本文介绍了使用ios5在iPad应用上测试互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在通过论坛搜索如何检查我的ipad应用程序中是否有互联网。我刚刚用其他视图控制器创建了一个简单的webview项目,我需要在互联网不可用时显示UIAlert消息。在我的情况下,它在我运行应用程序时显示消息。当我使用互联网运行应用程序然后停用互联网时,它不显示UIAlert消息,即如果我在视图之间切换,它不再显示没有互联网连接。

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.

我在我的项目中遵循了这种实现方式:(抱歉我的错误这是我所遵循的链接) http://mozymac.com/forums/f54/how-check-if- there-internet-connection-iphone-os-devices-595 / [这是新编辑的问题]

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]

除此之外我还经历了一些Stackoverflow论坛中的前几个问题,例如:如何检查iOS或OSX上的活动Internet连接?

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?

但每个人都有自己的版本。如果任何人有更多更新的ios5方法,xcode 4.2.1如何实现这一点对我有帮助。

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.

谢谢

推荐答案

在实际尝试在 UIWebView中加载请求之前,是否有理由要检查互联网连接

最佳做法是开始加载,并使用 UIWebViewDelegate / NURLConnectionDelegate 检查NSError以查看错误。如果网络出现故障,您将收到一个错误,其域名等于 NSURLErrorDomain 。错误代码将指出问题所在,请参阅 NSError代码 枚举。

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 to NSURLErrorDomain. The error code will indicate what the problem is, see the NSError 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.

示例

因为您使用的是 UIWebView 您应该实现以下委托方法以通知错误。

Since you are using a UIWebView you should implement the following delegate method to be notified of errors.

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    if (![[error domain] isEqualToString:NSURLErrorDomain]) {
        // not a nsurl error, take other appropriate action
        return;
    }

    NSInteger code = [error code];
    // show appropriate error to user, based on code
}

In这种委托方法你应该做任何你想要的东西。您可以自己重试请求,向用户显示消息或使用Apple提供的可访问性示例中的代码开始侦听可访问性更改。

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.

这篇关于使用ios5在iPad应用上测试互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆