确定iPhone上的互联网可用性? [英] Determining Internet Availability on iPhone?

查看:53
本文介绍了确定iPhone上的互联网可用性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iPhone应用中使用NSURLConnection如下:

I am using NSURLConnection in an iPhone app as so:

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest: request delegate: self];

请求已设置并正常运行,但我希望能够提供连接不是如果没有可用的连接,则向用户发送消息。有没有快速的方法来确定iPhone上是否有可用的Internet连接?

The request has been setup and works properly, but I want to be able to provide a "connection not available" message to the user if there isn't a connection available. Is there a quick way to determine whether an Internet connection is available on the iPhone?

在启动NSURLConnection后添加以下内容会非常简单:

Would something as simple as adding the following after initing the NSURLConnection:

if (conn) { // do normal processing } else { // display connection not available message }

提前致谢,
Ben

Thanks in advance, Ben

推荐答案

Raj,该代码有效,但并不总能创建所需的结果。

Raj, that code works, but doesn't always create the desired result.

iPhone上的TCP堆栈的工作方式与应该的方式截然不同被期望。使用可达性代码,有时会出现网络连接,但无法可靠地检测到。但是,启动MobileSafari然后重新尝试检查与可达性代码(Raj的代码)的连接将得到正确的结果。

The way that the TCP stack on the iPhone works is very different from what should be expected. With the "Reachability" code, sometimes a network connection will be present, but will not be reliably detected. However, launching MobileSafari then reattempting to check connectivity with "Reachability" code (Raj's code) will result in the correct result.

我发现最有效的方法网络连接是在应用程序加载时在单独的线程中运行NSURLConnection检查。拨打一个您知道会返回是的URL(即服务器上的HTML文件或其他内容)。然后检查以确保返回的结果等于静态文本。这样,你知道NSURLConnection堆栈正确地伸出,而不是Reachability代码不能很好地协调工作。

The way that I have found most effective in determining network connectivity is to run a NSURLConnection check when the application loads, in a separate thread. Make a call to a URL that you know will return something like "Yes" (i.e. HTML file on your server or something). Then check to be sure the returned result is equal to the static text. That way, you know that the NSURLConnection stack is reaching out properly, as opposed to the "Reachability" code that does not quite work consistently.

这篇关于确定iPhone上的互联网可用性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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