ios - UIWebview.loadURL() 但 URL 是自签名证书。 swift iOS

标签 ios swift uiwebview certificate

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

但是没有成功。


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


错误说:Safari cannot verify the IDENTITY

需要帮助绕过这个。


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

@implementation NSURLRequest (NSURLRequestWithIgnoreSSL) 

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

@end

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

最佳答案

最后得到的答案是:

第 1 步 >> 导入 SafariServices

第 2 步 >> 将 NSURLConnectionDelegate 与您的 ViewController 一起使用,即

类 ViewController:UIViewController, NSURLConnectionDelegate

第 3 步 >> 覆盖方法:

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

func connection(connection: NSURLConnection, willSendRequestForAuthenticationChallenge challenge: NSURLAuthenticationChallenge)

第 4 步 >> 转到 viewDidLoad 在 Web View 中加载您的 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 的大量页面以及 Google 的一些论坛。

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

关于ios - UIWebview.loadURL() 但 URL 是自签名证书。 swift iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36441194/

相关文章:

swift - fatal error : unexpectedly found nil while unwrapping an Optional value - using UITableView

ios - 如何防止 UIWebView 使页面内容适合屏幕宽度(既不缩放也不回流)?

ios - 从未打开的 ViewController 执行 Segue

iphone - 如何通过parentviewcontroller将数据从一个 View 传递到另一个 View

ios - 如何在应用程序和它的扩展之间共享一个类?

ios - UIPopoverController不断重新加载UIWebView

ios - UIWebview 中的大页面并不总是对角滚动

html - iPad/iPhone Safari div 未使用全宽

ios - 在Swift 3中没有使用Sinch接收通知

ios - 如何快速将base64转换为NSDATA