根据网络变化更新 iPhone 的可达性
编辑 23.5.11
我现在想知道我是否对此进行了过度设计。当我使用在线连接时,我会正确处理它 - 或者:
在后台显示进度或活动指示器,如果失败,我会显示一条合适的消息或
我调用另一个应用程序,Safari 或地图,然后它会进行自己的检查,如果没有连接,则会失败。这使得用户在另一个失败的应用程序中,这并不完美。
因此,如果我按照下面的建议进行操作,并且标准 Reachability 返回无法访问,然后我必须执行 NSURLConnection 以防收音机进入睡眠状态,那么我不确定这是否比仅仅尝试获取在线资源更好首先。
如果有人可以提供帮助 - 我将非常感激。我几乎准备好提交了,只需要正确处理即可。
我正在使用 Apple 的可达性示例,总体效果良好,除非移动或蜂窝连接进入睡眠状态然后再恢复,或者 wifi 断线后又恢复正常。然后依赖蜂窝连接。
当移动(蜂窝)连接没有改变时就可以了。这与我读过的评论一致,即当蜂窝无线电关闭时,您必须再次手动唤醒它。但是,我不知道该怎么做。
我需要设置 NSURLConnection 吗?一些示例代码会很棒。当我收到没有连接的通知时,我是否会发送 NSURLConnection 并告诉用户稍后重试,然后如果我收到回调表明其成功,则覆盖 Reachability 的关闭设置?
此外,每当似乎没有连接时,我是否需要这样做,以防万一 - 这似乎浪费资源,而事实可能并非如此。 egan iPod Touch 不在 Wi-Fi 覆盖范围内。
我还查看了 DDG 的替代方案,但看起来会有同样的问题。
任何有用的建议都将受到热烈欢迎。
谢谢,
克里斯。
Edit 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
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.
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.
If anyone can help - I'd be really grateful. I'm almost ready to submit, just need to get this right.
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.
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?
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.
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.
Thanks,
Chris.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能也不应该使用 Apple 的 Reachabilty 示例代码(或 SCNetworkReachabilityFlags)来确定网络资源是否可用或将可用。它们为您提供了有用的工具,可以向用户提供有关连接失败原因的反馈,以及确定是否以及何时重试连接。但是,如果您想访问网络资源,您只需提出请求即可。
不要根据可达性标志向用户显示“无网络连接”警报。执行网络请求,如果失败,请检查可达性标志和您自己的请求历史记录,看看是否应该告诉用户请求失败或静默重试。您不应该希望或不需要在每次网络故障时向用户发送垃圾邮件。如果请求失败,如果主机似乎可以访问并且仅在几次尝试后才向用户报告失败,或者当可达性标志表明您实际上已丢失网络连接而不仅仅是单个请求失败时,请考虑重试。
至于重新激活设备的无线电,您需要平衡尝试获取网络资源的愿望与允许设备关闭网络接口以节省电量的愿望。我不知道您的应用程序正在尝试执行什么操作,但我建议允许设备关闭电源,然后尝试连接以响应用户要求应用程序获取新数据的请求。我没有看到您想要重新激活无线电只是为了向用户提示某些网络资源当前可能或可能无法访问的情况。
请参阅https://devforums.apple.com/message/409618
另请看一下有关在使用外部应用程序加载资源之前检查资源的问题:https://devforums .apple.com/message/411329
对资源发出 HEAD 请求应该可以让您合理地了解外部应用程序是否能够加载该资源而不需要您下载大量数据(延迟仍然会不过,这是一个问题)。
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.
See https://devforums.apple.com/message/409618
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
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).