某些WKWebView链接无法点击

时间:2017-12-08 21:59:36

标签: ios swift wkwebview

我遇到了基于WKWebView的iOS应用程序的挑战:页面上的某些链接无法点击。当我使用Safari视图控制器尝试它们时,它们可以工作。请有人帮帮我吗?

这是我的源代码:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
    var webView: WKWebView! //declare the webview has to be optional
    var activityIndicator: UIActivityIndicatorView!


    override func loadView() {
        super.loadView()
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        webView.navigationDelegate = self
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let myURL = URL(string: "https://stolenandfound.com/")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)


        activityIndicator = UIActivityIndicatorView() //declare the progress indicator
        activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)

        self.activityIndicator.center = CGPoint(x:self.view.bounds.size.width/2.0,y: self.view.bounds.size.height/2.0);

        activityIndicator.autoresizingMask = (UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleRightMargin.rawValue) | UInt8(UIViewAutoresizing.flexibleLeftMargin.rawValue) | UInt8(UIViewAutoresizing.flexibleBottomMargin.rawValue) | UInt8(UIViewAutoresizing.flexibleTopMargin.rawValue))))

        //activityIndicator.center = CGPoint(x: UIScreen.main.bounds.size.width/2, y: UIScreen.main.bounds.size.height/2)
        activityIndicator.hidesWhenStopped = true

        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
        activityIndicator.color = UIColor.darkGray
        self.view.addSubview(activityIndicator)
    }



    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        activityIndicator.startAnimating()
    }

    func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
        print("it is an error")
        activityIndicator.stopAnimating()
        let alert = UIAlertController(title: "Network Error", message: "You have no internet coonection", preferredStyle: .alert)

        let restartAction = UIAlertAction(title: "Reload page", style: .default, handler: { (UIAlertAction) in
            self.viewDidLoad()
        })

        alert.addAction(restartAction)
        present(alert, animated: true, completion: nil)
    }




    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        activityIndicator.stopAnimating()
    }

    @IBAction func backButton(_ sender: UIBarButtonItem) {
        webView.goBack()
    }


    @IBAction func refreshButton(_ sender: UIBarButtonItem) {
        webView.reload()
    }

}

1 个答案:

答案 0 :(得分:3)

如果链接在Safari的新标签页中打开,则必须实施WKUIDelegate函数func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?并在那里处理请求。此时,您可以在现有webView中加载请求,或者创建新的webView或在Safari中打开它。

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        guard let theURL = navigationAction.request.url else {
           return nil
        }
        if loadInCurrentWebview{
            _ = webView.load(navigationAction.request) //loads the link in the current webView
        } else if loadInNewWebview{
           return WKWebView()
        }else { 
            //loads in safari
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(theURL, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(theURL)
            }
        }
    return nil
}