Reach and response NSURLSession to discover your Internet connection

I am creating errors for my API calls and would like to check the status when the Internet connection is not available.

dataTaskWithRequest seems to do a great job of this and returns an NSError with code -1009 with a description of the Internet connection. I don’t have to wait until the request comes out.

When would I like to use the Framework Reachability Framework? Thanks

+5
source share
1 answer

I did a bit of work and found my answer in Apple Docs.

The recommended recommendation is to try to connect. If there is a connection problem, it gives you an NSError object that you should use to verify connectivity.

Apple recommends using Reachability only as a way to diagnose errors and further debug known issues. It looks like you should only use accessibility for detection when the network returns to the network after a failure.

"Always try to connect. Do not try to guess if a network service is available and do not cache this definition.

If the connection fails, use the SCNetworkReachability API to help diagnose the cause of the failure.

source: https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/WhyNetworkingIsHard/WhyNetworkingIsHard.html#//apple_ref/doc/uid/TP40010220-CH13-SW3

"When a task completes, the NSURLSession object calls the delegate method URLSession:task:didCompleteWithError: either with an error object or with nil (if the task completed successfully).

If the task fails, most applications should retry the request until the user cancels the download or the server returns an error indicating that the request will never be completed. However, your application should not retry immediately. Instead, it should use reachability APIs to determine if the server is available, and should only make a new request when it receives a notification that availability has changed. "

A source:

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/NSURLSessionConcepts/NSURLSessionConcepts.html

+12
source

Source: https://habr.com/ru/post/1242897/


All Articles