简体   繁体   English

如何使用 iOS 可达性

[英]How to use iOS Reachability

I'm developing an iPhone app that uses the network.我正在开发一个使用网络的 iPhone 应用程序。 The iPhone communicate with my server via HTTP request and should work on WiFi and 3G. iPhone 通过 HTTP 请求与我的服务器通信,应该可以在 WiFi 和 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)我目前使用NSURLConnection initWithRequest向我的服务器发送异步请求并获得响应(但我很快就会转向使用ASIHTTPRequest库)

I understood that with this kind of apps(apps that requires internet connection) I should (must?) use 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.在苹果的例子中,他们检测主机、WiFi 和 3G 的网络问题,并向用户显示适当的消息。
Is this the main purpose of Reachability, to show the user a message? Reachability 的主要目的是向用户显示消息吗? 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?例如,如果NSURLConnaction请求失败,我是否需要以某种方式使用可达性来重新发送请求?

What is the proper use of Reachability?什么是可达性的正确使用?

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 ?使用reachabilityWithHostName就足够了吗,还是我还需要reachabilityForLocalWiFireachabilityForInternetConnection

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 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请求失败,我是否需要以某种方式使用可达性来重新发送请求?

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.假设我要下载 10 个文件。 When any file fails the download process due to no Internet connection, I save them to an array of failed downloads.当任何文件由于没有 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?什么是可达性的正确使用?

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.您可以在没有 Reachability 类的多个实例的情况下做到这一点。 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 是否有连接。

Is it enough to use reachabilityWithHostName or do I need also reachabilityForLocalWiFi and reachabilityForInternetConnection ?使用reachabilityWithHostName就足够了吗,还是我还需要reachabilityForLocalWiFireachabilityForInternetConnection

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)可能会有用。

Reachability is an example project that Apple has made. Reachability是 Apple 制作的示例项目。 People use this as an API over the SystemConfiguration framework.人们将其用作 SystemConfiguration 框架上的 API。 As you already have seen there are methods to check if a host is reachable and so on.正如您已经看到的,有一些方法可以检查主机是否可达等。

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.我使用 Reachabilty 项目的方式是,我创建了一个带有类方法的类,如果我从中请求数据的主机可用,该方法返回一个布尔值。 If it is then the method returns YES and if it is not, it returns NO (obviously).如果是,则该方法返回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.现在,在我使用连接的应用程序中,您需要/应该检查是否可以按照 Apple 文档所述启动连接。 I have a simple if and then show an appropriate alert message that the request could not be completed at the moment.我有一个简单的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.您不必使用 Reachablitiy 类中的所有方法,使用其中一个就足够了。 It is documented what the different methods offer and when they are ment to be used in the header file.记录了不同方法提供的内容以及何时在头文件中使用它们。

Remember to include the SystemConfiguration framework.请记住包含SystemConfiguration框架。

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.它告诉您是否可以通过 WiFi 访问,或者您是否只能通过移动数据访问。 There is no guarantee that it is correct.不能保证它是正确的。 If it says you have a WiFi connection, that connection can be lost one second later.如果它说您有 WiFi 连接,该连接可能会在一秒钟后丢失。 The only way to find whether accessing a URL will work or not is to make that access and see what happens.确定访问 URL 是否有效的唯一方法是进行访问并查看会发生什么。 There are situations where access will work when Reachability says it doesn't and vice versa.在某些情况下,访问会起作用,而可达性却说它不起作用,反之亦然。

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).以下是我使用 Reachibility 的目的:由于没有互联网连接而导致下载失败后,我将在 Reachability 检测到更改时开始重试(实际上是几秒钟后;在 Reachability 表示 WiFi 恢复后,URL 访问通常会立即失败)。 After downloads failed where Mobile Data was disallowed, check if Mobile Data is available then ask the user to allow use of Mobile Data.在不允许移动数据的情况下下载失败后,检查移动数据是否可用,然后要求用户允许使用移动数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM