【问题标题】:WKWeb View does finish loading of page in swift iOS?WKWebView 是否在 swift iOS 中完成了页面加载?
【发布时间】:2019-01-27 13:07:40
【问题描述】:

我创建了一个 WKWebView。它加载 MSB 的 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)
    }
}

【问题讨论】:

    标签: ios swift wkwebview wkwebviewconfiguration


    【解决方案1】:

    您需要为该特定网址添加例外 (SecTrustSetExceptions)。因为它的签名身份和你的WKWebView有一些冲突。

    我在我的应用程序中也遇到了同样的问题,并通过使用下面的 sn-p 成功解决了。

    试试这个:

        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>
    

    【讨论】:

    • 谢谢。但我得到以下错误规定 发生 SSL 错误,无法与服务器建立安全连接。如何忽略 ssl?
    • 此解决方案仅用于为 ssl 故障添加异常。如果您已尝试实现此代码 sn-p,您的 SSL 错误应该可以通过此修复。
    • 然后检查您的 info.plist 以了解您所做的 ATS 配置。如果可能的话,你可以在这里分享网址。
    • NSAllowsArbitraryLoadsInWebContent 这样做不允许我通过非 ssl 服务器发出 http 请求。我需要添加域例外吗?
    • @Techiee 添加此 NSAllowsArbitraryLoadsInWebContent 并将其设置为 true 后,它将允许您加载 http 资源。我想你误解了这一点。
    猜你喜欢
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多