问题
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. (Users can choose if they upload media only over WiFi or WiFi and 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.
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. I've looked and looked but haven't seen anyone talking about this. It seems it hasn't been a very widespread need.
回答1:
You can refer the apple reachability code
回答2:
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.
来源:https://stackoverflow.com/questions/7507756/getting-reachability-changed-notifications-in-the-background