swift - NSURLConnection The certificate for this server is invalid -


recently i've been trying connect test server of mine using self-signed ssl certificate using nsurlsession.sharedsession().datataskwithrequest()

now error:

the certificate server invalid. might connecting server pretending “...” put confidential information @ risk. 

i've been searching web how solve it. of them advised using 1 of these:

func connection(connection: nsurlconnection, canauthenticateagainstprotectionspace protectionspace: nsurlprotectionspace) -> bool  func connection(connection: nsurlconnection, didreceiveauthenticationchallenge challenge: nsurlauthenticationchallenge)  func connection(connection: nsurlconnection, willsendrequestforauthenticationchallenge challenge: nsurlauthenticationchallenge)    func urlsession(session: nsurlsession, didreceivechallenge challenge: nsurlauthenticationchallenge, completionhandler: (nsurlsessionauthchallengedisposition, nsurlcredential!) -> void) 

now, when ran app noticed

func connection(connection: nsurlconnection, willsendrequestforauthenticationchallenge challenge: nsurlauthenticationchallenge)  

ran.

this code:

func connection(connection: nsurlconnection, willsendrequestforauthenticationchallenge challenge: nsurlauthenticationchallenge) {     // trusting , not trusting connection host: self-signed certificate     challenge.sender.usecredential(nsurlcredential(fortrust: challenge.protectionspace.servertrust), forauthenticationchallenge: challenge)     challenge.sender.continuewithoutcredentialforauthenticationchallenge(challenge)      if(challenge.protectionspace.authenticationmethod == nsurlauthenticationmethodservertrust)     {         let trust = challenge.protectionspace.servertrust         let cred = nsurlcredential(fortrust: trust)          challenge.sender.usecredential(cred, forauthenticationchallenge: challenge)     } } 

yet keep getting nsurlconnection/cfurlconnection http load failed (kcfstreamerrordomainssl, -9813).

aha! apparently i've had let urlconnection = nsurlconnection(request: request, delegate: self) bit before haven't noticed...

and changed nsurlsession.sharedsession().datataskwithrequest()
let task = nsurlsession(configuration: nsurlsessionconfiguration.defaultsessionconfiguration(), delegate: self, delegatequeue: nsoperationqueue.mainqueue()).datataskwithrequest(request) , left in place delegate methods... did :)


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -