代码之家  ›  专栏  ›  技术社区  ›  TechChain

WKWeb视图是否在swift iOS中完成页面加载?

  •  1
  • TechChain  · 技术社区  · 8 年前

    我创建了一个 WKWebView 完成装载 没有人打电话。我已经为web视图添加了所有的代理。但它肯定不起作用。整个过程在iPhone的web浏览器中运行良好。

    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)
        }
    }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Milan Manwar    8 年前

    您需要添加异常( 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!))
        }
    }
    

        <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoadsInWebContent</key>
        <true/>
    </dict>
    
    推荐文章