ios - WKWebView 确实在 swift iOS 中完成了页面加载?

标签 ios swift wkwebview wkwebviewconfiguration

我创建了一个WKWebView。它加载 MSB 即的 url。我的学校钱。它加载几乎所有的付款网址。但是在一个 url didFinishLoad 没有被调用。我已经为 web View 添加了所有代表。但肯定是行不通的。整个过程在 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

导航的委托(delegate)方法

// 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)
    }
}

最佳答案

您需要为该特定网址添加异常(exception) (SecTrustSetExceptions)。因为它的签名身份和你的 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>

关于ios - WKWebView 确实在 swift iOS 中完成了页面加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51947069/

相关文章:

ios - 增加标签栏项目的宽度以适合图像

ios - 如何修复 iOS 上 Cordova 3.1 的键盘问题?

ios - wkwebview 不会在 iframe 中显示 youtube 视频

ios - 为什么 iPhone 6 及更高版本 wkwebview 不接受底部点击?

ios - evaluateJavascript 没有执行函数

iphone - 舍入到 "beautiful"值

ios - Today Widget 扩展的问题

ios - 在 UITableView Swift 的所有部分上方添加 UIView

swift - 如何使用 AVFoundation 制作视频填充框

ios - 如何将多个相同节点添加到 View 中?