我应该在每个UIViewController中监听可达性更新吗? [英] Should I listen for reachability updates in each UIViewController?

查看:135
本文介绍了我应该在每个UIViewController中监听可达性更新吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多Reachability示例,其中人们只在可达性状态发生变化时显示消息。

但最近,我在Foursquare应用程序中看到,每次用户尝试执行操作时,它们都会显示一条消息需要互联网连接。

我认为这是一个更强大和更好的用户体验提醒用户他没有互联网不能做任何事情。主要是因为用户可以在应用程序之间切换,做其他事情并忘记他回来时没有连接。

一旦他们恢复连接,我就可以看到他们从互联网上获取数据并刷新用户界面。

I see a lot of Reachability examples where people only display a message when reachability status changes.
But recently, I saw in Foursquare app that they display a message every time the user try to make an action requiring an Internet connection.
I think this is more robust and a better UX to remind the user he can't do anything without Internet. Mainly because users can switch between apps, do something else and forget he has no connection when he comes back.
Also as soon as they get the connection back I can see that they fetch data from the Internet and refresh the UI.

我真正想要的是做到这一点的最好方法。如何做到这一点?

他们是否有一个通用的UIViewController,每次需要连接时检查可达性?

或者他们在每个互联网请求之前都有一种代理类取消请求并显示消息?
你们是如何处理的?

What I am really looking for is the best way to do this. How this is done?
Do they have a general UIViewController that checks for reachability each time it needs a connection?
Or do they have a kind of proxy class before each Internet request that cancels the request and display a message? How you guys are dealing with that?

谢谢。

编辑:

我提出的解决方案是使用AFNetworking,它也在框中提供可达性状态。

基本上我创建了一个AFHTTPClient并在其上设置了一个可达性回调块听取状态变化。 AFHTTPClient对象是应用程序范围的(一种单例)。 (事实上​​,每个主机我有一个AFHTTPClient,我需要到达a.com,b.com ......)。

The solution I came up with is using AFNetworking which also provide reachability status in the box.
Basically I created an AFHTTPClient and set a reachability callback block on it to listen to status changes. The AFHTTPClient object is application wide (kind of a singleton). (in fact I have one AFHTTPClient per host I need to reach a.com, b.com ...).

然后当我需要执行请求时,我创建了一个新的AFHTTPRequestOperation(在我的情况下是AFJSONRequestOperation),然后我将它排入我的AFHTTPClient对象。
在操作的故障块中,我检查是否可以使用AFHTTPClient的 networkReachabilityStatus 属性访问主机。如果无法访问,我会显示一条消息,表明用户没有互联网连接。

Then when I need to perform a request I create a new AFHTTPRequestOperation (AFJSONRequestOperation in my case) and I enqueue it on my AFHTTPClient object. In the failure block of the operation I check to see if the host is reachable with the networkReachabilityStatus property of the AFHTTPClient. If it's unreachable I display a message that there is no internet connection to the user.

我把它包起来,所以每次创建操作时我都不必这样做。所以现在在应用程序中,每当用户尝试在没有连接时做某事时,他都会收到一条消息,记住他没有互联网访问权限。

我还使用可达性回调来重新加载数据屏幕一旦我得到连接(或者更确切地说,我应该有一个连接)。

我不知道这是否是最好的做法,但我认为很高兴知道该应用程序负责重新加载一旦有新连接,就会有重要数据。

I wrapped that up so I don't have to do this each time I create an operation. So now in the app, each time the user try to do something when there is no connection he got a message remembering him that he has no internet access.
I also use the reachability callback to reload data on a screen once I get the connection back (or rather once I am supposed to have a connection).
I don't know if it's best practice but I think it's nice to know that the app takes care of reloading important data as soon as a new connection is available.

如果某人对示例代码感兴趣,我可以提供。

If someone is interested by a sample code I can provide it.

推荐答案

在今年的WWDC演讲中,舞台上的Apple工程师建议用户永远不要将应用程序互联网访问基于Reachability示例应用程序状态。通常可达性不提供完整的信息(它基于复杂的机制),工程师提供的建议如下:

On a WWDC talk this year the Apple engineer on stage recommended users to never base the application internet access on the Reachability example app status. Often reachability doesn't provide a complete information (it is based on a complex mechanism) and the suggestion provided by the engineer was this:


  1. 尝试进行互联网连接,无论是可达性状态;然后根据成功/失败结果设置您的UI提示

  2. 如果由于网络问题而失败,则注册到Reachability并在Reachability给出绿灯时再次重试;当您想要从失败状态自动恢复时需要这样做

  3. 在任何情况下都允许用户强制重试,无论可达性状态如何。如果成功,请立即重置UI提示。

Apple工程师所说的完全正确:通常你可以在控制台中看到当互联网连接完全存活时,记录可达性故障消息。

What the Apple engineer said is perfectly true: quite often you can see in the console log Reachability failure messages while the internet connection is perfectly alive.

其他事情:没有比状态栏中显示的更好的网络提示:如果你有那里有Wi-Fi图标,3G / 4G图标,蜂窝场强。

Other thing: there is no better "network hint" than the one displayed in the status bar: if you have there the wi-fi icon, the 3G/4G icon, the cellular field strength.

回到原来的问题:没有绝对更好的方法来管理这些东西,这在很大程度上取决于应用程序架构。如果您更喜欢将网络内容集中在专用类(不是UIViewController而是NSObject子类)中,那么为最新的Internet连接后更新为成功/失败的类定义只读属性是有意义的。使用服务器应用程序(对谷歌或苹果等其他服务器进行ping操作是没有意义的:首先它不优雅,其次问题可能来自服务于应用程序的服务器而不是设备互联网连接状态!)。

Returning to your original question: there is no absolute better way to manage this stuff, this depends heavily on the application architecture. In case you prefer to concentrate your networking stuff in a dedicated class (not a UIViewController but a NSObject subclass) then it could make sense to define a read-only property for that class that is updated with "success/fail" after latest internet connection with the servers app (it makes no sense to ping other servers like Google or Apple: first of all it's not elegant, second the issue may come from your the servers that serve the app and not the device internet connection status!).

@property (readonly) BOOL lastConnectionToMyServerSuccess


然后您的视图控制器可以注册(通过KVO或中央通知)通过显示图标或其他方式更改和更新其属性(我重复:让用户尝试手动连接到互联网)。视线控制器应该在看不见时(viewWillDisappear:)或卸载(viewDidLoad:)或dealloc'd从KVO取消注册。

Then your view controllers can register (by KVO or by central notification) to this property changes and update their UI accordingly by showing an icon or else (I repeat: leave the user the possibility to try manually to connect to the internet). View controllers should unregister from KVO when out of sight ("viewWillDisappear:") or unloaded ("viewDidLoad:") or dealloc'd.

当然这会增加一些额外的复杂性。例如:你使用应用程序,互联网灯是绿色的。然后你暂停它,做一些其他事情,几分钟后你回到应用程序。在这种情况下,应用应该ping您的服务器以再次恢复互联网指示灯状态,因为几分钟之后网络状况可能已经改变(例如您在火车上)。在任何情况下,所有加载的视图控制器都将从网络专用类中获取KVO通知并自行更新。

Of course this adds some extra complexities. E.g.: you use the app, the internet light was green. Then you suspend it, do something else and after a few minutes your return to the app. In such case the app should ping your servers to restore the internet light status again, as after a few minutes network conditions could have changed (e.g. you are on a train). In any case all loaded view controllers will get the KVO notification from the network dedicated class and update themselves.

这篇关于我应该在每个UIViewController中监听可达性更新吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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