如何使用 iOS 可达性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11177066/
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
How to use iOS Reachability
提问by Eyal
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)
我正在开发一个使用网络的 iPhone 应用程序。iPhone 通过 HTTP 请求与我的服务器通信,应该可以在 WiFi 和 3G 上工作。
我目前使用NSURLConnection initWithRequest
向我的服务器发送异步请求并获得响应(但我很快将转向使用ASIHTTPRequest
库)
I understood that with this kind of apps(apps that requires internet connection) I should (must?) use Reachability.
我知道对于这种应用程序(需要互联网连接的应用程序),我应该(必须?)使用 Reachability。
After searching the web and looking at Apple's Reachability example code i still don't understand some basic stuff:
在网上搜索并查看 Apple 的 Reachability 示例代码后,我仍然不明白一些基本的东西:
What is the main purposes of 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?
在苹果的例子中,它们检测主机、WiFi 和 3G 的网络问题,并向用户显示适当的消息。
这是 Reachability 的主要目的,向用户显示消息吗?还是我需要将它用于其他更实际的场景?例如,如果NSURLConnaction
请求失败,我是否需要以某种方式使用 Reachability 来重新发送请求?
What is the proper use of Reachability?
什么是 Reachability 的正确用法?
Is it common to use only oneinstance 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
?
应用启动时只使用一个实例,然后监听网络变化是否常见?或者我应该在每次网络请求之前检查自己的可达性状态?
是否足够使用reachabilityWithHostName
或我还需要reachabilityForLocalWiFi
和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?
还有一件事,我知道苹果可以拒绝使用网络且不使用可达性的应用程序。我应该实施
哪些“必须”做的方法?
仅通知用户当前没有互联网就足够了吗?
回答by Omar Abdelhafith
Reachability
is a network helper utility class, its used to get various informations about the connection status
Reachability
是一个网络助手实用程序类,用于获取有关连接状态的各种信息
What is the main purposes of Reachability?
可达性的主要目的是什么?
Reachability is used to query the network status, and to register your listeners to get informed when connectivity changes.
可达性用于查询网络状态,并注册您的侦听器以在连接发生变化时获得通知。
Is this the main purpose of Reachability, to show the user a message?
这是 Reachability 的主要目的,向用户显示消息吗?
No, its main usage is to test if there is Internet connectivity, or to get notified if the connectivity changes
不,它的主要用途是测试是否有 Internet 连接,或者在连接发生变化时收到通知
For example if
NSURLConnection
request has failed do I need to use Reachability somehow to resend the request?
例如,如果
NSURLConnection
请求失败,我是否需要以某种方式使用 Reachability 来重新发送请求?
Yes, you could use it. For example, what I normally do in my project is to save all the requests that have been made to a remote server.
是的,你可以使用它。例如,我在我的项目中通常做的是将所有已向远程服务器发出的请求保存起来。
Let's 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.
假设我想下载 10 个文件。当任何文件由于没有 Internet 连接而导致下载过程失败时,我会将它们保存到一系列失败的下载中。
Then, when Reachability informs me that the Internet connection has been restored, I iterate through this array and start the download process again.
然后,当 Reachability 通知我 Internet 连接已恢复时,我遍历此数组并再次开始下载过程。
What is the proper use of Reachability?
什么是 Reachability 的正确用法?
It depends, on your patterns and needs.
这取决于您的模式和需求。
Is it common to use only one instance when app launch, and then listen to network changes?
应用启动时只使用一个实例,然后监听网络变化是否常见?
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.
是的,这就是我所做的。在我的项目中,我只有 1 个下载管理器类的实例,而这个类具有 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.
您可以在没有多个 Reachability 类实例的情况下做到这一点。我通常做的是在我的下载管理器中有一个方法,它告诉我是否有连接使用 Reachability。
Is it enough to use
reachabilityWithHostName
or do I need alsoreachabilityForLocalWiFi
andreachabilityForInternetConnection
?
是否足够使用
reachabilityWithHostName
或我还需要reachabilityForLocalWiFi
和reachabilityForInternetConnection
?
I'm not sure about this one, but what I normally do is to test connectivity on all the means. I don't differentiate between 3G or WiFi, but there are some implementations where this info (WiFi or 3G) could be useful.
我不确定这个,但我通常做的是测试所有方式的连接性。我不区分 3G 或 WiFi,但在某些实现中,此信息(WiFi 或 3G)可能有用。
回答by Andreas
Reachabilityis an example project that Apple has made. People use this as an API over the SystemConfiguration framework. As you already have seen there are methods to check if a host is reachable and so on.
Reachability是 Apple 制作的一个示例项目。人们将其用作 SystemConfiguration 框架上的 API。正如您已经看到的,有一些方法可以检查主机是否可达等。
The way I use the Reachabilty project is that I have made a class with a class method that returns a boolean if the host I am requesting data from is available. If it is then the method returns YES
and if it is not, it returns NO
(obviously).
我使用 Reachabilty 项目的方式是,我创建了一个带有类方法的类,如果我从中请求数据的主机可用,该类将返回一个布尔值。如果是,则该方法返回YES
,如果不是,则返回NO
(显然)。
Now, in the application where I am making use of connections you need/should check if it is possible to start a connection as the Apple documentation states. I have a simple if
and then show an appropriate alert message that the request could not be completed at the moment.
现在,在我使用连接的应用程序中,您需要/应该检查是否可以按照 Apple 文档所述启动连接。我有一个简单的if
然后显示一个适当的警报消息,该请求目前无法完成。
There are no requirements that you should automatically try again if the request could not be carried out the first time. The main purpose of this is to keep your application away from crashing, and at the same time give the user an message that it could not be done.
如果第一次无法执行请求,则没有要求您应该自动重试。这样做的主要目的是防止您的应用程序崩溃,同时向用户发出无法完成的消息。
I do not use the notifications myself, but that is because I am not interested in monitoring whether or not connection can be carried out. This is something that you will have to decide by your application demands.
我自己不使用通知,但那是因为我对监控是否可以进行连接不感兴趣。这是您必须根据您的应用需求来决定的。
You are not forced to use all the methods in the Reachablitiy class, it is sufficient to use one of them. It is documented what the different methods offer and when they are ment to be used in the header file.
您不必使用 Reachablitiy 类中的所有方法,使用其中一种就足够了。它记录了不同方法提供的内容以及何时在头文件中使用它们。
Remember to include the SystemConfiguration
framework.
记住要包含SystemConfiguration
框架。
回答by gnasher729
Reachability makes a best guess whether the internet is reachable or not. It tells you whether you have access through WiFi, or whether you have access only through Mobile Data. There is no guarantee that it is correct. If it says you have a WiFi connection, that connection can be lost one second later. The only way to find whether accessing a URL will work or not is to make that access and see what happens. There are situations where access will work when Reachability says it doesn't and vice versa.
可达性可以最好地猜测互联网是否可达。它会告诉您是否可以通过 WiFi 访问,或者是否只能通过移动数据访问。不能保证它是正确的。如果它说您有 WiFi 连接,则该连接可能会在一秒钟后丢失。确定访问 URL 是否有效的唯一方法是进行访问并查看会发生什么。在某些情况下,当 Reachability 说它不起作用时,访问会起作用,反之亦然。
Here's what I use Reachibility for: After downloads failed because there was no internet connection, I'll start retrying when Reachability detects a change (actually a few seconds later; URL access often fails immediately after Reachability says WiFi comes back). After downloads failed where Mobile Data was disallowed, check if Mobile Data is available then ask the user to allow use of Mobile Data.
以下是我使用 Reachibility 的用途:由于没有互联网连接而导致下载失败后,当 Reachability 检测到更改时,我将开始重试(实际上是几秒钟后;在 Reachability 说 WiFi 恢复后,URL 访问通常会立即失败)。在不允许移动数据的情况下下载失败后,检查移动数据是否可用,然后要求用户允许使用移动数据。