UIWebview.loadURL()但URL是自签名证书.Swift iOS

Paw*_*wan 6 certificate uiwebview ios swift

我有一个URL https://203.xxx.xxx.xxx.此URL仅用于我们的测试目的.

我想在Swift2.0中的UIWebview中加载此URL.可惜 !证书已过期.

我在Android中也做了同样的事情,但在Swift中遇到了问题.

请指导.....


到现在为止我做了什么!!


01. Defined AppTransportSecurity in info.plist

    <key>NSAppTransportSecurity</key>
            <dict>
                <key>NSAllowsArbitraryLoads</key>
                <true/>
            </dict>
Run Code Online (Sandbox Code Playgroud)
  1. 尝试使用连接(canAuthenticateAgainstProtectionSpace)和连接(willSendRequestForAuthenticationChallenge)

但没有成功.


当我在Safari中加载相同的URL时会发生什么?


错误说:Safari无法验证IDENTITY

绕过这个需要帮助.


还尝试了一些链接 :(这可能是解决方案)

@implementation NSURLRequest (NSURLRequestWithIgnoreSSL) 

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
    return YES;
}

@end
Run Code Online (Sandbox Code Playgroud)

但是如何在Swift 2.0中实现这个呢?如果我错了,请在Swift中正确引导.

Paw*_*wan 8

终于得到了答案:

第1步>> import SafariServices

步骤2 >>使用NSURLConnectionDelegate和您的ViewController即

class ViewController:UIViewController, NSURLConnectionDelegate

第3步>>覆盖方法:

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

func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge)
Run Code Online (Sandbox Code Playgroud)

第4步>> GOTO viewDidLoad在Webview中加载URL的位置,并进行如下更改:

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);
Run Code Online (Sandbox Code Playgroud)

步骤5 >>使用shouldStartLoadWithRequest GOTO webView并返回true.如果你返回false,你永远不会得到结果.

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

 return true
}
Run Code Online (Sandbox Code Playgroud)

第6步>>更新功能:

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

        return true;
    } 
Run Code Online (Sandbox Code Playgroud)

第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)

    }
Run Code Online (Sandbox Code Playgroud)

参考资料:Swift的开发人员参考,Stackoverflow的Lot of Pages以及Google的一些论坛.

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

  • 在第4步中,为什么同时具有`request`和`requestObj`? (2认同)