将UIWebView委托迁移到WKWebView委托方法

时间:2017-09-12 08:24:05

标签: ios swift swift3 xcode8

我正在努力将UIWebView迁移到WKWebView。我已经更改了所有Delegate方法。我需要WKWebView委托方法等于下面的UIWebView委托方法。该应用程序运行正常。但登录会话未保留

UIWebView:

    extension WebViewController: UIWebViewDelegate {

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    guard let url = request.url else {
       return true
    }

    guard !url.absoluteString.contains("data:application/pdf") else {
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.action,
                                                            target: self,
                                                            action: #selector(share(sender:)))
        return true
    }

    guard url.pathExtension != "pdf" else {
        let safariVC = SFSafariViewController(url: url)
        safariVC.modalPresentationStyle = .popover
        present(safariVC, animated: true, completion: nil)
        return false
    }

    guard url.isLogin() == false else {
        AppDelegate.navigationController.signOut(.sessionOnly)
        return false
    }

    guard let mobileSite = url.asMobileSite() else {
        return true
    }

    let mobileRedirect = URLRequest(url: mobileSite)
    webView.loadRequest(mobileRedirect)
    return false

}

func webViewDidStartLoad(_ webView: UIWebView) {
    numberOfDidStartLoads += 1
}

func webViewDidFinishLoad(_ webView: UIWebView) {
    numberOfDidStartLoads -= 1
}

func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
    numberOfDidStartLoads -= 1
}
}

我尝试了以下代码并使会话过期。

extension WebViewController: UIWebViewDelegate {

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (_: WKNavigationActionPolicy) -> Void) {

    guard let url = navigationAction.request.url else {
        decisionHandler(.allow)
        return
    }

    guard !url.absoluteString.contains("data:application/pdf") else {
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.action,
                                                            target: self,
                                                            action: #selector(share(sender:)))
        decisionHandler(.allow)
        return
    }

    guard url.pathExtension != "pdf" else {
        let safariVC = SFSafariViewController(url: url)
        safariVC.modalPresentationStyle = .popover
        present(safariVC, animated: true, completion: nil)
        decisionHandler(.cancel)
        return
    }

    guard url.isLogin() == false else {
        AppDelegate.navigationController.signOut(.sessionOnly)
        decisionHandler(.cancel)
        return
    }

    guard let mobileSite = url.asMobileSite() else {
        decisionHandler(.allow)
        return
    }

    let mobileRedirect = URLRequest(url: mobileSite)
    webView.load(mobileRedirect)
    decisionHandler(.cancel)
    return

    decisionHandler(.allow)

}

    func webViewDidStartLoad(_ webView: UIWebView) {
        numberOfDidStartLoads += 1
    }

    func webViewDidFinishLoad(_ webView: UIWebView) {
        numberOfDidStartLoads -= 1
    }

    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
        numberOfDidStartLoads -= 1
    }
}

请帮我解决此问题。我在将代码从UIWebView更改为WKWebView时犯了一些错误。

3 个答案:

答案 0 :(得分:6)

您可能需要在代码中实现以下内容,这意味着尝试使用 WKNavigationDelegate 协议而不是使用 UIWebViewDelegate 协议。当您使用会话处理时,我猜您错过了最重要的功能之一。

   func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        print(#function)
        completionHandler(.performDefaultHandling,nil)
    }

AuthChallengeDisposition 有不同类型,例如

public enum AuthChallengeDisposition : Int {


    case useCredential

    case performDefaultHandling

    case cancelAuthenticationChallenge

    case rejectProtectionSpace
}

完成 WKNavigationDelegate 协议

  extension ViewController: WKNavigationDelegate{
    func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
        print(#function)
    }

    func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
        print(#function)
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print(#function)
    }

    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        print(#function)
    }

    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        print(#function)
    }

    func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
        print(#function)
    }

    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print(#function)
    }

    func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        print(#function)
        completionHandler(.performDefaultHandling,nil)
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        print(#function)
        decisionHandler(.allow)
    }

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

答案 1 :(得分:2)

我猜您可以使用webView(_:decidePolicyFor:decisionHandler:)并阻止/取消或允许请求。这应该以同样的方式起作用。

免责声明: 我还没有测试过,我会在找到一些时间后立即这样做。

答案 2 :(得分:1)

分析你的代码,我发现了一个永远不会达到的声明" return"以前叫过。

陈述是:

decisionHandler(.allow)

您可以在函数的最后一行代码中找到它:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (_: WKNavigationActionPolicy) -> Void)

你有这个方法:

func webViewDidStartLoad(_ webView: UIWebView) {
    numberOfDidStartLoads += 1
}