Search code examples
iosobjective-cwifinsurlconnection

Error Domain=kCFErrorDomainCFNetwork Code=-1009


I'm developing an APP which downloads data from a URL. Randomly the error

"Error Domain=kCFErrorDomainCFNetwork Code=-1009"

is shown. I know the error means I have lost Wifi connection. In fact, the wifi logo disappears from my Ipad. After turning off and on the WiFi the connection comes back and I can restart downloading data. I noticed that if I use the AppStore or Safari while the connection is lost it reconnects automatically. The question is: what should I do with my app to get the same behavior as AppStore or Safari so I can reconnect automatically?


Solution

  • It isn't automatic. At a high level, the way you do it is:

    1. When you get an error, use Apple's Reachability sample code class to create an object that watches for reachability changes to the host in question. Stick it in an array somewhere for safe keeping.

    2. When the reachability class notifies you that reachability for that host has changed, try the request again.

    3. If that request succeeds, stop listening for reachability for that host and remove the reachability object from the array.

    For more info, see: https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html