auto login issue with HTTPS site with unstrusted SSL Certificate in swift

546 views Asked by At

I am facing difficulty to login to HTTPS site with unstrusted SSL Certificate in swift. I can view the page inside my UIWebView by using NSURLConnectionDelegate methods. I can auto login to this website using a web browser e.g: URL - https://mywebsite.com:8003/home.html?session_id=sessionVariable . But not from UIWEbView

Here is my code inside ViewDidLoad:

 let websiteURL = "https://mywebsite.com:8003/home.html?session_id=\(Session_Id)"

        print(websiteURL)
        let url = NSURL (string: websiteURL)
        let urlRequest = NSURLRequest(URL: url!)
        let urlConnection:NSURLConnection = NSURLConnection(request: urlRequest, delegate: self)!
        self.webviewInstance.loadRequest(urlRequest)

My delegate functions are

func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{
    print("canAuthenticateAgainstProtectionSpace method Returning True")
    return true
}


func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge){

    print("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)")

    if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust  {
        print("send credential Server Trust")
        let credential = NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!)
        challenge.sender!.useCredential(credential, forAuthenticationChallenge: challenge)

    }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic{
        print("send credential HTTP Basic")
        let defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession)
        challenge.sender!.useCredential(defaultCredentials, forAuthenticationChallenge: challenge)

    }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
        print("send credential NTLM")

    } else{
        challenge.sender!.performDefaultHandlingForAuthenticationChallenge!(challenge)
    }
}

I have aded changes in my plist file for the keys NSAppTransportSecurity , NSAllowsArbitraryLoads etc.

I could interact with JSON calls with this server. But the auto login from a webView is not working. Please advice.

1

There are 1 answers

0
dgatwood On

Those delegate methods don't get called except for requests that you create yourself, and I don't think cookies are shared between web views and normal app requests, either.

Your best bet would be to create a custom NSURLProtocol that intercepts http/https requests (from the web view and elsewhere), adds a custom header (so you can recognize resent requests whenever your protocol class sees them again) and then resends the request using your own NSURLConnection object.

For details, see Using a custom URL scheme with UIWebView