swift - 如何在 uiwebview (swift) 中调用 https url?

标签 swift ssl https uiwebview

我在 UIWEBView 中有带有自签名证书的 https url,但它不起作用。

Error is NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)...

如何通过 swift 允许 uiwebview 中的任何认证

你有办法解决这个问题吗?

最佳答案

我找到了一种方法来做到这一点,但我认为这不是一个好方法。

第 1 步------在 override func viewDidLoad(){} 部分使用 NSURLConnection 请求 = NSURLRequest(URL:url)

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

第 2 步------使用 NSURLConnection 委托(delegate)

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/

相关文章:

java - 更新 TeamCity Server SSL 证书后断开 TeamCity Build Agent

ssl - Selenium 和 HTTPS/SSL

arrays - 在 Swift 中将两个数组转换为字典

swift - 在 swift 上显示用户位置

ssl - 如何在 weblogic 12c 中控制密码套件

Java相互认证-客户端服务器

ios - swift 不确定layoutIfNeeded setNeedsUpdateConstraints或updateConstraints setNeedsLayout

ios - 我想在下次触发通知时检索

python - httplib2 socket.sslerror : (-1, 'SSL exception' )

ssl - 什么是 ssl 证书中的数据加密