如何覆盖wkwebview超链接操作表

Jed*_*ant 5 ios swift

当您长按WkWebview中的超链接时,将获得一个操作表。长按时,我想用我自己的一组选项覆盖该操作表,但否则会表现正常。我可以参加长按活动,但我不知道如何:

  1. 防止显示默认操作表
  2. 长按结束时,防止Web视图导航到URL。
  3. 获取Web视图直接指向的基础href URL或html标记本身以及相关属性(例如ID)。

Jed*_*ant 1

这是覆盖菜单的有效解决方案。我仍然想获得一些 html 属性,但还不知道该怎么做。

override func loadView() {
    super.loadView()

    let contentController = WKUserContentController();
    let userScript = WKUserScript(
        source: "redHeader()",
        injectionTime: WKUserScriptInjectionTime.AtDocumentEnd,
        forMainFrameOnly: true
    )
    contentController.addUserScript(userScript)
    contentController.addScriptMessageHandler(
        self,
        name: "callbackHandler"
    )
    let config = WKWebViewConfiguration()
    config.userContentController = contentController
    self.webView = WKWebView(frame: self.view.frame, configuration: config)
    self.webView.navigationDelegate = self
    self.view = self.webView
    let lpress = UILongPressGestureRecognizer(target: self, action: "webViewLongPressed:")
    lpress.delegate = self
    self.webView.scrollView.addGestureRecognizer(lpress)
}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

func webViewLongPressed(sender:UILongPressGestureRecognizer!) {
    longpress = true
    if (sender.state == UIGestureRecognizerState.Ended) {
        print("Long press Ended")
        //This is where everything starts to happen
    } else if (sender.state == UIGestureRecognizerState.Began) {
        print("Long press detected.")

    }

}

func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
    if let myUrlStr : String = navigationAction.request.URL!.absoluteString {

        if myUrlStr.lowercaseString.rangeOfString("/book/") != nil {
            /* Do not allow links to be tapped */
            var parts = myUrlStr.componentsSeparatedByString("/")
            let id = Int(parts[4])
            if navigationAction.navigationType == .LinkActivated && longpress == true {

                decisionHandler(.Cancel)
                let ac = actionMenu(self, id: id!, user_id: 1)
                self.presentViewController(ac, animated: true) {

                }
                longpress = false
                return
            }
        }
    }
    decisionHandler(.Allow)

}
//Build action sheet
func actionMenu(sender: UIViewController, id: Int, user_id: Int) -> UIAlertController {

    let alertController = UIAlertController(title: "Title", message: "Some message.", preferredStyle: .ActionSheet)

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in

    }
    alertController.addAction(cancelAction)
    let someAction = UIAlertAction(title: "Some action", style: .Default) { (action) in
        //do something, call a function etc, when this action is selected
    }
    alertController.addAction(someAction)

    return alertController
}
Run Code Online (Sandbox Code Playgroud)