【问题标题】:UIWebview.loadURL() but URL is a self-signed certificate. Swift iOSUIWebview.loadURL() 但 URL 是自签名证书。斯威夫特 iOS
【发布时间】:2016-07-26 06:28:17
【问题描述】:

我有一个网址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>
  1. 尝试使用连接(canAuthenticateAgainstProtectionSpace)和连接(willSendRequestForAuthenticationChallenge)

但是没有成功。


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


错误提示:Safari cannot verify the IDENTITY

需要帮助以绕过此问题。


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

@implementation NSURLRequest (NSURLRequestWithIgnoreSSL) 

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

@end

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

【问题讨论】:

    标签: ios swift uiwebview certificate


    【解决方案1】:

    终于得到了答案:

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

    第 4 步 >> GOTO viewDidLoad 在 Web 视图中加载 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);
    

    第 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 的 Lot of Pages 以及 Google 的一些论坛。

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

    【讨论】:

    • 第四步,为什么requestrequestObj都有?
    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2014-08-13
    • 2017-07-22
    • 2017-03-24
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多