Is this a specialized use of NSURLConnection to handle self-signed certificates convertible to NSURLSession?

I have a self-signed certificate in a virtual machine that I use to test my service. Using the answers found in UIWebView to browse self-signed sites (no private api, not NSURLConnection) - is this possible? I was able to write valid 2.0 fast code. Xcode 7 tells me that NSURLConnection deprecated, and I should use NSURLSession . None of my attempts to port this code have failed, and none of the usual conversion scripts described in the other answers apply.

If I create a new NSURLSession to handle the authentication call using my delegation methods, the rest of the load still occurs on the sharedSession and therefore fails.

 var authRequest : NSURLRequest? = nil var authenticated = false var trustedDomains = [:] // set up as necessary func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { if !authenticated { authRequest = request let urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)! urlConnection.start() return false } else if isWebContent(request.URL!) { // write your method for this return true } return processData(request) // write your method for this } func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) { if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust { let challengeHost = challenge.protectionSpace.host if let _ = trustedDomains[challengeHost] { challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!), forAuthenticationChallenge: challenge) } } challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge) } func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) { authenticated = true connection.cancel() webview!.loadRequest(authRequest!) } 
+5
source share
1 answer

I managed to suppress the deprecation warning by adding this line before the first method. I would prefer a solution that replaces NSURLConnection, but in the absence of this, it would have to do.

 // hide NSURLConnection deprecation @available(iOS, deprecated=9.0) 
0
source

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


All Articles