如何使用iOS Reachability [英] How to use iOS Reachability

查看:151
本文介绍了如何使用iOS Reachability的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用网络的iPhone应用程序。 iPhone通过HTTP请求与我的服务器通信,应该可以在WiFi和3G上运行。

我目前使用 NSURLConnection initWithRequest 向我的服务器发送异步请求得到答复(但我很快就会开始使用 ASIHTTPRequest 库)

I'm developing an iPhone app that uses the network. The iPhone communicate with my server via HTTP request and should work on WiFi and 3G.
I currently use NSURLConnection initWithRequest to send async requests to my server and get responses (but I will soon move to work with ASIHTTPRequest library)

我理解这种应用程序(需要互联网连接的应用程序)我应该(必须?)使用可达性。

I understood that with this kind of apps(apps that requires internet connection) I should (must?) use Reachability.

在搜索网页并查看Apple的Reachability示例代码后,我仍然不了解一些基本内容:

After searching the web and looking at Apple's Reachability example code i still don't understand some basic stuff:

可达性的主要目的是什么?

在Apple的示例中,他们检测到主机,WiFi和3G的网络问题,并向用户展示一个合适的消息。

这是Reachability的主要目的,向用户显示一条消息吗?
或者我是否需要将它用于其他更实际的场景?例如,如果 NSURLConnaction 请求失败,我是否需要以某种方式使用Reachability重新发送请求?

In apple's example they detect network issues with the host, WiFi and 3G and present the user with an appropriate message.
Is this the main purpose of Reachability, to show the user a message? Or do I need to use it for other more practical scenarios? For example if NSURLConnaction request has failed do I need to use Reachability somehow to resend the request?

可达性的正确用途是什么?

在应用启动时只使用一个实例,然后收听网络变化?或者我应该在每次网络请求之前检查自己的可达性状态?

是否足以使用 reachabilityWithHostName 或者我还需要 reachabilityForLocalWiFi reachabilityForInternetConnection

Is it common to use only one instance when app launch, and then listen to network changes? Or should I check myself the reachability status before every network request?
Is it enough to use reachabilityWithHostName or do I need also reachabilityForLocalWiFi and reachabilityForInternetConnection?

还有一件事,我知道苹果可以拒绝使用网络但不使用可达性的应用。

我应该实施哪些必须方法?

仅通知用户当前没有互联网是否足够?

One more thing, I understood apple can reject apps that use the network and don't use Reachability.
What are the "must" do methods I should implement?
Will it be enough to just notify the user that currently there is no internet?

推荐答案

可达性是一个网络助手实用程序类,用于获取有关连接状态的各种信息

Reachability is a network helper utility class, its used to get various informations about the connection status


可达性的主要目的是什么?

What is the main purposes of Reachability?




  • 可达性用于查询网络状态

  • 并注册听众以便在连接更改时获得通知


  • 这是Reachability的主要目的,向用户显示消息吗?

    Is this the main purpose of Reachability, to show the user a message?

    当然不,它的主要用途是测试是否有互联网连接,或者获取noti如果连接发生变化

    No of course, its main usage is either to test if there is internet connectivity, or to get notified if the connectivity changes


    例如,如果NSURLConnaction请求失败,我是否需要使用
    可达性以某种方式重新发送请求?

    For example if NSURLConnaction request has failed do I need to use Reachability somehow to resend the request?

    是的,你可以使用它,例如我通常在我的项目中做的是保存所有已经发出的请求远程服务器,假设我想下载10个文件,

    Yes you could use it, for example what i normally do in my project is to save all the request that has been made to a remote server, lets say i want to download 10 files,

    当任何文件由于没有互联网连接而导致下载过程失败时,我将它们保存到下载失败的数组中,

    When any file fails the download process due to no internet connection, i save them to an array of failed downloads,

    当可达性告诉我互联网连接已经恢复时,我会遍历这个数组并再次开始下载过程

    When reachability informs me that the internet connection has been restored, i iterate through this array and start the download process again


    可达性的正确用途是什么?

    What is the proper use of Reachability?

    这取决于您的模式和需求。

    It depends, on your patterns and needs.


    在app启动时只使用一个实例,然后听
    来进行网络更改吗?

    Is it common to use only one instance when app launch, and then listen to network changes?

    是的,这就是我的工作,在我的项目中,我只有1个下载管理器类的实例,而且这个类只有一个活动的可达性实例

    Yes that is what i do, in my projects i only have 1 instance of a download manager class, and this class has the only alive instance of Reachability


    或者我应该在每个网络
    请求之前检查自己的可达性状态?

    Or should I check myself the reachability status before every network request?

    你可以做到这一点,没有多个可达性类实例,我通常做的是在我的下载管理器中有一个方法告诉我如果有连接就使用可达性。

    You can do that without having multiple instance of Reachability classes, what i normally do, is to have a method inside my download manager that tells me using Reachability if there is connection or not.


    使用reachabilityWithHostName是否足够,还是需要
    reachabilityForLocalWiFi和reachabilityForInternetConnection?

    Is it enough to use reachabilityWithHostName or do I need also reachabilityForLocalWiFi and reachabilityForInternetConnection?

    我不确定这个,但我通常做的是测试连接的所有方法,我不区分3g或wifi,但有一些实现这个信息(wifi或3g)可能有用

    Am not sure about this one, but what i normally do is to test connectivity on all the means, i dont differentiate between 3g or wifi, however there are some implementation that this info (wifi or 3g) could be useful

    这篇关于如何使用iOS Reachability的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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