如何在uiwebview(swift)中调用https url? [英] How to call https url in uiwebview (swift)?

查看:316
本文介绍了如何在uiwebview(swift)中调用https url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have https url with self-sign certication in UIWEBView ,but it does not work.


错误是NSURLConnection / CFURLConnection HTTP加载失败
(kCFStreamErrorDomainSSL,-9813)...

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

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

how can I to allow any certication in uiwebview by swift

你有什么想法可以解决这个问题吗?

Do you have any idea that fix this problem?

推荐答案

我发现一种方法,但我认为这不是一个好方法。

i find a way to do this,but i think this is not a good way.

第1步------在覆盖func viewDidLoad(){}部分使用NSURLConnection
request = NSURLRequest(URL:url)

step 1------use NSURLConnection in override func viewDidLoad(){} section request = NSURLRequest(URL:url)

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

第2步------使用NSURLConnection委托

step 2------ use the NSURLConnection delegate to

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];
}






它可以工作


it can work

抱歉,我的英语技能很差

sorry for my poor english skill

这篇关于如何在uiwebview(swift)中调用https url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆