1

我创建了一个WKWebView。它加载 MSB 的 url,即。我学校的钱。它加载了几乎所有的付款网址。但是在一个 url didFinishLoad没有被调用。我已经为网络视图添加了所有代表。但它肯定不起作用。整个过程在 iPhone 的网络浏览器中运行良好。

下面是我在下面添加的代码

let preferences = WKPreferences()
        preferences.javaScriptEnabled = true
        let configuration = WKWebViewConfiguration()
        configuration.preferences = preferences
        self.webView = WKWebView(frame: frame, configuration: configuration)
        self.view.addSubview(webView)
        self.webView.navigationDelegate = self
        self.webView.uiDelegate = self

导航的委托方法

// MARK: - WKWebView Delegation
extension PaymentWebViewController:WKNavigationDelegate {

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        DILog.print(items: "didFinish")
        self.hideLoader()
        if let strUrl = webView.url?.absoluteString {
            self.readRedirectUrl(stringUrl: strUrl)
        }
    }

    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        DILog.print(items: "faluere")
        self.hideLoader()
    }

    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        DILog.print(items: "didStatrt")
        self.showLoader()
        if let strUrl = webView.url?.absoluteString {
            DILog.print(items: "URL IS \(strUrl)")
        }
    }

    func webView(_ webView: WKWebView,
                 didReceive challenge: URLAuthenticationChallenge,
                 completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void)
    {
        if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust)
        {        DILog.print(items: "Auth Channlenge1")
            let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
            completionHandler(.useCredential, cred)
        }
        else
        {        DILog.print(items: "Auth Channlenge2")
            completionHandler(.performDefaultHandling, nil)
        }
    }

    func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
        DILog.print(items: "webViewWebContentProcessDidTerminate")

    }

    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        DILog.print(items: "didCommit")

    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        DILog.print(items: "decidePolicyFor Action")
        decisionHandler(WKNavigationActionPolicy.allow)
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        DILog.print(items: "decidePolicyFor response ")

        decisionHandler(.allow)
    }
}
4

1 回答 1

0

您需要SecTrustSetExceptions为该特定 url 添加异常 ()。由于其签名身份和您的WKWebView.

我在我的应用程序中也面临同样的问题,并通过使用以下代码段成功解决。

尝试这个:

    extension PaymentWebViewController: WKNavigationDelegate {
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        decisionHandler(.allow)
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        decisionHandler(.allow)
    }

    func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        let serverTrust = challenge.protectionSpace.serverTrust
        let exceptions = SecTrustCopyExceptions(serverTrust!)
        SecTrustSetExceptions(serverTrust!, exceptions)
        completionHandler(.useCredential, URLCredential(trust: serverTrust!))
    }
}

希望您已经在 info.plist 中进行了设置:

    <key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
</dict>
于 2018-08-21T11:26:18.733 回答