0

我正在从迁移UIWebViewWKWebView. 需要在WKWebView所有内容中加载 HTML 内容工作正常,但问题是在单击任何链接后无法在外部 safari 浏览器中打开链接WKWebView。我用下面的代码

班级

class CTFSPolicyAndTermsViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {}

出口和WKWebView变量

  @IBOutlet weak var webViewContainer: UIView!
  var webView: WKWebView!

viewDidLoad方法

    override func viewDidLoad() {
    super.viewDidLoad()
    webView = WKWebView()
    self.webView.uiDelegate = self
    self.webView.navigationDelegate = self

    let webConfiguration = WKWebViewConfiguration()
    webView =  WKWebView(frame: .zero, configuration: webConfiguration)
    webView.translatesAutoresizingMaskIntoConstraints = false
    webViewContainer.addSubview(webView)
   [webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor),
    webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor),
    webView.leftAnchor.constraint(equalTo: webViewContainer.leftAnchor),
    webView.rightAnchor.constraint(equalTo: webViewContainer.rightAnchor)].forEach  { 
     anchor in
    anchor.isActive = true
    }

用于加载 HTML 内容

     if(self.isTermsMode)
        {

            let path:String = Bundle.main.path(forResource: "abc", ofType: "html")!
            let text = try? String(contentsOfFile: path, encoding: String.Encoding.utf8)
            webView!.loadHTMLString(text!, baseURL: nil)
        }

决定政策

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if let url = navigationAction.request.url {
            print(url)
            UIApplication.shared.open(url)  
            decisionHandler(.cancel)
        } else {
        decisionHandler(.allow)
       }
    }

decidePolicyFor方法应该调用但它没有执行我无法找到问题

4

1 回答 1

0

我错过了订单

let webConfiguration = WKWebViewConfiguration()
    webView =  WKWebView(frame: .zero, configuration: webConfiguration)
    webView.translatesAutoresizingMaskIntoConstraints = false
    webViewContainer.addSubview(webView)
    [webView.topAnchor.constraint(equalTo: webViewContainer.topAnchor),
     webView.bottomAnchor.constraint(equalTo: webViewContainer.bottomAnchor),
     webView.leftAnchor.constraint(equalTo: webViewContainer.leftAnchor),
     webView.rightAnchor.constraint(equalTo: 
      webViewContainer.rightAnchor)].forEach  { anchor in
        anchor.isActive = true
    }
    self.webView.uiDelegate = self
    self.webView.navigationDelegate = self
于 2020-03-30T13:50:00.987 回答