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

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

问题描述

编辑 23.5.11

我现在想知道我是否过度设计了这个.当我使用在线连接时,我会正确处理它 - 要么:

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 或 Maps,然后它们会进行自己的检查,如果没有连接就会失败.这让用户只能使用其他失败的应用程序,这并不完美.

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.

因此,如果我按照我在下面的建议进行操作并且标准 Reachability 返回无法访问,然后我必须执行 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.

如果有人可以提供帮助 - 我将不胜感激.我快要准备提交了,只需要把它做好.

我正在使用 Apple 的 Reachability 示例,除移动或蜂窝连接进入睡眠状态然后又恢复或 wifi 运行然后依赖蜂窝连接时,总体上效果良好.

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 吗?一些示例代码会很棒.当我收到没有连接的通知时,我是否会发送 NSURLConnection 并告诉用户稍后再试,然后如果我收到一个回调说它成功,覆盖 Reachability 的关闭设置?

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?

此外,我是否需要在似乎没有连接的情况下执行此操作,以防万一 - 这似乎很浪费资源,而实际上可能并非如此.例如,不在 wifi 范围内的 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.

谢谢,

克里斯.

推荐答案

您不能也不应该使用 Apple 的 Reachabilty 示例代码(或 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.

不要根据可达性标志向您的用户显示无网络连接"警报.执行您的网络请求,如果失败,请检查可达性标志和您自己的请求历史记录,看看您是否应该告诉您的用户请求失败或静默重试.您不应该或不需要在每次网络故障时向用户发送垃圾邮件.如果请求失败,请考虑重试,如果主机似乎可以访问并且仅在几次尝试后才向用户报告失败,或者当可达性标志表明您实际上已经失去了网络连接而不仅仅是单个请求失败时.

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

了解主机是否可用的唯一方法是尝试连接它.如果该连接失败,您可以使用可达性向用户提供反馈,并指导您的重试机制,但使用可达性预检连接并不是一个好主意.

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.

这里的关键问题是可达性使用本地信息来确定其结果.这并没有说明更广泛的互联网的状态.源服务器可能已关闭,或者您和源服务器之间的任意数量的链接可能已关闭,并且可达性会很高兴地说它是可达的.那时您将尝试连接,但连接将失败.因此,无论如何您都必须处理错误,这意味着您最好不要进行预检,而让您的标准错误处理也涵盖这种情况.

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/message/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天全站免登陆