我在uiwebview中有带自我签名认证的https URL,但它不起作用。
错误为nsurlconnection/cfurlconnection http load failed
(kcfstream错误域ssl,-9813)…
我怎样才能允许通过swift在uiwebview中进行任何认证?
你知道怎么解决这个问题吗?

最佳答案

我找到了一个办法,但我认为这不是一个好办法。
步骤1——在override func viewdidload()部分中使用nsurlConnection
请求=nsurlrequest(url:url)

    let urlConnection:NSURLConnection = NSURLConnection(request: request, delegate: self)!

步骤2------使用nsurlconnection委托
func connection(connection: NSURLConnection, didFailWithError error: NSError){
    println("didFailWithError")
}

func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace) -> Bool{
    println("canAuthenticateAgainstProtectionSpace")
    //return [protectionSpace.authenticationMethodisEqualToString:NSURLAuthenticationMethodServerTrust];
    return true
}

func connection(connection: NSURLConnection, didReceiveAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
    println("did autherntcationchallenge = \(challenge.protectionSpace.authenticationMethod)")

    //if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust && challenge.protectionSpace.host == "myDomain.com" {

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

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

    }else if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodNTLM{
        println("send credential NTLM")
        if challenge.previousFailureCount > 0 {
            //如果连续两次验证未通过,则终止, 还需要返回自定义出错界面
            //handle bad credentials scenario
        }

        var defaultCredentials: NSURLCredential = NSURLCredential(user: "username", password: "password", persistence:NSURLCredentialPersistence.ForSession)
        challenge.sender.useCredential(defaultCredentials, forAuthenticationChallenge: challenge)
        }

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

/*
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) {


}*/
func connection(connection: NSURLConnection, didCancelAuthenticationChallenge challenge: NSURLAuthenticationChallenge){
    println("didCancelAuthenticationChallenge")
}
/*
- (void)connection: (NSURLConnection *)connection willSendRequestForAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}*/

func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse){
    println("-----received response");



    // remake a webview call now that authentication has passed ok.

    //_authenticated =YES;

    //[_webloadRequest:_request];
   webView.loadRequest(request)


    // Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)

    //[_urlConnectioncancel];
}

它可以工作
抱歉我的英语水平太差了

关于swift - 如何在uiwebview(swift)中调用https url?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29047132/

10-16 11:00
查看更多