网络的变化更新iPhone可达 [英] Updating iPhone Reachability on Network Changes

查看:192
本文介绍了网络的变化更新iPhone可达的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改11年5月23日

我现在想知道我是否在这个工程。当我使用的在线连接,我处理得当 - 无论是:

I'm now wondering whether I'm over engineering this. When I use an online connection, I handle it properly - either:


  • 在一个进度或活动的指标显示,如果失败,我展示一个合适的消息的背景或

  • in the background with a progress or activity indicator showing and if it fails, I display a suitable message OR

我调用另一个应用程序,无论是Safari或地图然后将尽自己的检查,如果没有连接失败。这使得在发生故障的其他应用程序,这是不完美的寿用户。

I call another application, either Safari or Maps which will then do its own check and fail if there is no connection. That leaves the user in that other app that has failed, which is not perfect tho.

所以,如果我做我以下建议和标准可达返回无法到达,然后我必须做一个NSURLConnection的情况下收音机是睡着了,那么我不知道这是比任何只是想变得更好摆在首位的在线资源。

So if I do what I'm suggesting below and standard Reachability returns not reachable and I then have to do a NSURLConnection in case the radios have gone to sleep, then I'm not sure that's any better than just trying to get the online resource in the first place.

如果有人能帮助 - 我会非常感激。我几乎准备提交,只需要得到这个权利。

我使用的是苹果的样品可达性一般较好的效果,除了在移动​​或蜂窝连接进入睡眠状态,然后回来或去无线,然后蜂窝连接的依赖。

I'm using Apple's Reachability sample to generally good effect except for when the mobile or cellular connection goes to sleep and then comes back or the wifi goes and the cellular connection is then relied on.

其优良当移动(蜂窝)连接没有改变。这与我读过的意见线,当蜂窝无线电关闭你必须手动重新唤醒。不过,我看不出如何做到这一点。

Its fine when the mobile (cellular) connection isn't changing. This is in line with comments I've read, that when the cellular radio switches off you have to wake it up again manually. However, I can't see how to do that.

我是否需要设置一个NSURLConnection的?一些示例code将是巨大的。当我得到通知说没有联系,我会送NSURLConnection的,并告诉用户在某一时刻再次尝试,然后如果我得到一个回调说其成功,覆盖了从可达性设置?

Do I need to setup a NSURLConnection? Some sample code would be great. When I get a notification that there is no connection, would I send the NSURLConnection and tell the user to try again in a moment and then if I get a callback to say its successful, override the off setting from Reachability?

另外,我会需要做的,每当人们似乎没有什么联系,以防万一它是 - 这似乎浪费资源​​时,它可能不会是。 e.g.an的iPod Touch,这不是在无线网络覆盖范围。

In addition, would I need to do that whenever there seems to be no connection, just in case its that - this seems wasteful of resources when it may well not be that. e.g.an iPod Touch that's not in wifi reach.

我也看了 DDG 的替代品,但看起来,这将有同样的问题。

I've also looked at DDG's alternative, but it looks like that will have the same issue.

任何有帮助的建议将受到热烈的欢迎。

Any helpful suggestions would be warmly welcomed.

谢谢,

克里斯。

推荐答案

可以,也不应该使用苹果的Reachabilty样本code(或SCNetworkReachabilityFlags),以确定是否一个网络资源或将可用。他们给你有用的工具给有关为什么连接失败用户反馈并确定是否以及何时重试连接。但是,如果您要访问的网络资源,你应该只问它。

You cannot and should not use Apple's Reachabilty sample code (or the SCNetworkReachabilityFlags) to determine if a network resource is or will be available. They give you useful tools for giving user feedback about why a connection failed and for determining if and when to retry a connection. However if you want to access a network resource you should just ask for it.

不要present用户与基于可达性标志无网络连接警报。执行您的网络请求,如果失败检查可达性标志和你自己的请求的历史,看看你应该告诉你的用户请求失败或静静地重试。你不应该想要或需要的垃圾邮件与每一个网络故障用户。如果请求不考虑重试,如果主机似乎到达,只有一对夫妇尝试或后报告故障给用户时,可达性标志表明您实际上已失去了你的网络连接,而不只是有一个请求失败。

Don't present your users with "no network connection" alerts based on the reachability flags. Perform your network request and if it fails check the reachability flags and your own history of requests to see if you should tell your user that the request failed or silently retry. You shouldn't want or need to spam the user with every network failure. If a request fails consider retrying it if the host seems to be reachable and only report a failure to the user after a couple of attempts or when the reachability flags suggest that you have actually lost your network connection and not just had a single request fail.

至于重新激活设备的无线电你需要平衡你的愿望,尝试允许网络接口断电设备以节约电源获取网络资源。我不知道您的应用程序正在试图做的,但我建议允许设备断电,然后试图响应要求的应用程序来获取新数据的用户进行连接。我没有看到的情况下,你会想重新无线电只是为了给用户一些网络资源可以或不可以当前可到达的提示。

As for reactivating the device's radios you'll need to balance your desire to try to fetch network resources with allowing the device to power down the network interfaces to save power. I don't know what your app is trying to do but I'd recommend allowing the device to power down and then trying to connect in response to a user asking the app to fetch new data. I don't see a case where you would want to reactivate a radio just to give the user a hint that some network resource may or may not currently be reachable.

请参阅 https://devforums.apple.com/message/409618

要知道一个主机是否是唯一的办法是尝试连接到它。如果连接失败,您可以使用可达性,提供反馈给用户,并引导你的重试机制,但使用可达性preflight的连接是不是一个好主意。

The only way to know whether a host is available is to try to connect to it. If that connection fails, you can use reachability to provide feedback to the user, and to guide your retry mechanism, but using reachability to preflight a connection is not a good idea.

这里的关键问题是可达性使用本地信息来确定它的结果。这并不是说对更广泛的互联网的状态的任何事情。原始服务器可能已关闭,或任意数量的你和源服务器之间的链接可能是下降,可达性将高兴地说,这是到达。在这一点上你要尝试连接,而且连接将失败。所以,你将不得不反正来处理错误,这意味着你还不如不做preflight,让你的标准错误处理封面这种情况下也是如此。

The key issue here is that reachability uses local information to determine its results. This doesn't say anything about the state of the wider Internet. The origin server might be down, or any number of links between you and the origin server might be down, and reachability will happily say that it's reachable. At that point you're going to try to connect, and the connection will fail. So you're going to have to handle the error anyway, which means you might as well not do the preflight and let your standard error handling cover this case as well.

另外看看这个问题,就检查了使用外部应用程序加载之前的资源: https://devforums.apple .COM /消息/ 411329

Also take a look at this question regarding checking for a resource before loading it using an external application: https://devforums.apple.com/message/411329

制作了资源的HEAD请求应该给你一个合理的想法,如果外部应用程序将能够加载它,而不需要你下载的数据的显著量(延迟仍然是,虽然关注)。

Making a HEAD request for the resource should give you a reasonable idea if the external app will be able to load it without requiring you to download a significant amount of data (latency will still be a concern though).

这篇关于网络的变化更新iPhone可达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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