简体   繁体   English

在后台获取可达性更改的通知

[英]Getting Reachability changed notifications in the background

Im developing an app that uploads media to a server. 我正在开发一个将媒体上传到服务器的应用程序。 I use Andrew Donoho's Reachability class to determine if I've got Reachability via WiFi, WWAN or if it is not reachable. 我使用安德鲁·多诺霍(Andrew Donoho)的“可达性”类来确定我是否通过WiFi,WWAN获得了可达性,或者它是否无法达到。 (Users can choose if they upload media only over WiFi or WiFi and WWAN). (用户可以选择是否仅通过WiFi或WiFi和WWAN上传媒体)。 If the application enters the background, uploads should continue. 如果应用程序进入后台,则应该继续上传。 But if the user loses WiFi connectivity while the app is in the background, uploads should stop. 但是,如果用户在后台运行时失去了WiFi连接,则上传应停止。

This is why I need to find a way to get Reachability changed notifications in the background, so I can stop uploading if the user loses wifi connectivity. 这就是为什么我需要找到一种方法来在后台获取“可达性更改”通知的原因,因此如果用户失去wifi连接,我可以停止上传。 I've looked and looked but haven't seen anyone talking about this. 我看了看,但是还没看到有人在谈论这个。 It seems it hasn't been a very widespread need. 似乎并没有很广泛的需求。

您可以参考苹果可达性代码

Instead of depending on Reachability I would handle the error of not being able to reach the server in general. 而不是依赖于可达性,我将处理通常无法访问服务器的错误。 Regardless of whether or not internet is available. 无论互联网是否可用。 Depending on how the server side is implemented you may need to reupload all of the data or continue on uploading the remaining part of the data. 根据服务器端的实现方式,您可能需要重新上传所有数据或继续上传其余数据。 In any case updating the bookkeeping locally about what was uploaded or that you will need to retry this in the future can be done in the error handling delegate. 无论如何,可以在错误处理委托中本地更新有关上载内容的簿记或以后需要重试的内容。

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

This delegate is on NSURLConnection and will be called when an error occurs like a timeout or loss of connection. 该委托位于NSURLConnection上,并在发生超时或连接断开之类的错误时被调用。

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

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