UIWebview.loadURL()但URL是自签名证书。斯威夫特iOS

UIWebview.loadURL()但URL是自签名证书。斯威夫特iOS,ios,swift,uiwebview,certificate,Ios,Swift,Uiwebview,Certificate,我有一个网址。此URL仅用于我们的测试目的 我想在Swift2.0的UIWebview中加载此URL。但是,唉!证书过期了 我在Android上也成功地做到了这一点,但在Swift上有问题 请引导 我到现在都做了些什么 但如何在Swift 2.0中实现这一点? 如果我错了,请用Swift正确引导。最终得到的答案是: 步骤1>导入Safaris服务 步骤2>>将NSURLConnectionLegate与ViewController一起使用,即 class ViewController:UIV

我有一个网址。此URL仅用于我们的测试目的

我想在Swift2.0的UIWebview中加载此URL。但是,唉!证书过期了

我在Android上也成功地做到了这一点,但在Swift上有问题

请引导


我到现在都做了些什么


但如何在Swift 2.0中实现这一点?
如果我错了,请用Swift正确引导。

最终得到的答案是:

步骤1>
导入Safaris服务

步骤2>>将NSURLConnectionLegate与ViewController一起使用,即

class ViewController:UIViewController,NSURLConnectionLegate

步骤3>>覆盖方法:

func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace?) -> Bool

func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge)
步骤4>>转到viewDidLoad在Webview中加载URL的位置,并按以下方式进行更改(&D):

let url = NSURL (string: URL)//where URL = https://203.xxx.xxx.xxx
let requestObj = NSURLRequest(URL: url!)
var request: NSURLRequest = NSURLRequest(URL: url!)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
connection.start()
YOUR_WEBVIEW.loadRequest(requestObj);
步骤5>>转到带有shouldStartLoadWithRequest的webView并返回true。如果返回false,则永远不会得到结果

func webView(IciciWebView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool
{
//Do whatever you want to do.

 return true
}
步骤6>>更新功能:

func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace?) -> Bool
    {
        print("In canAuthenticateAgainstProtectionSpace");

        return true;
    } 
步骤7>>更新功能:

func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) 
    {
        print("In willSendRequestForAuthenticationChallenge..");
        challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!), forAuthenticationChallenge: challenge)
        challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge)

    }
参考资料:Swift的开发者参考资料,Stackoverflow的很多页面,以及谷歌的一些论坛


这些步骤解决了我的问题&现在我可以在web视图中加载自签名URL。

在步骤4中,为什么同时使用
request
requestObj
func connection(connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: NSURLProtectionSpace?) -> Bool
    {
        print("In canAuthenticateAgainstProtectionSpace");

        return true;
    } 
func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge) 
    {
        print("In willSendRequestForAuthenticationChallenge..");
        challenge.sender!.useCredential(NSURLCredential(forTrust: challenge.protectionSpace.serverTrust!), forAuthenticationChallenge: challenge)
        challenge.sender!.continueWithoutCredentialForAuthenticationChallenge(challenge)

    }