ios - Detecting DNS failure with NSURLSession background config? -
is possible detect dns lookup failures when using nsurlsession background configuration ?
if use default config , completion handler, dns resolution failure reported in error parameter. if use background configuration however, failure never reported , delegate methods never called.
the apple documentation nsurlsessiontaskdelegate protocol says :
server errors not reported through error parameter. errors delegate receives through error parameter client-side errors, such being unable resolve hostname or connect host.
which suggests should seeing dns failure message there. code below illustrates behaviour. have tested on ios 8.4, , 9 on both device , simulator , searched dev forums, , using popular search engines come empty. i'm sure must missing simple.
import uikit @uiapplicationmain class appdelegate: uiresponder, uiapplicationdelegate, nsurlsessiontaskdelegate { var window: uiwindow? func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool { let bgsesh = nsurlsession(configuration: nsurlsessionconfiguration.backgroundsessionconfigurationwithidentifier("foobarbazqwux"), delegate: self, delegatequeue: nil) let dfsesh = nsurlsession(configuration: nsurlsessionconfiguration.defaultsessionconfiguration(), delegate: nil, delegatequeue: nil) let url = nsurl(string: "http://foobarbaz.qwzx")! let = dfsesh.downloadtaskwithurl(url) { (url:nsurl?, resp:nsurlresponse?, err:nserror?) -> void in print(err) /* optional(error domain=nsurlerrordomain code=-1003 "a server specified hostname not found." userinfo=0x146e8050 { nserrorfailingurlstringkey=http://foobarbaz.qwzx/, _kcfstreamerrorcodekey=8, nserrorfailingurlkey=http://foobarbaz.qwzx/, nslocalizeddescription=a server specified hostname not found., _kcfstreamerrordomainkey=12, nsunderlyingerror=0x14693ab0 "the operation couldn’t completed. (kcferrordomaincfnetwork error -1003.)" }) */ } a.resume() let b = bgsesh.downloadtaskwithurl(url) b.resume() return true } func urlsession(session: nsurlsession, task: nsurlsessiontask, didcompletewitherror error: nserror?) { // never runs, dns failure not reported background config :-( print("didcompletewitherror: \(task.taskidentifier) \(error)") } // ... other delegate methods ... }
Comments
Post a Comment