How to override wkwebview hyperlink action table

When you click on a hyperlink in WkWebview for a long time, you get an action sheet. I want to override this action sheet with my own set of parameters when it is pressed for a long time, but otherwise behave normally. I can get a long press event, but I don’t know how:

  • Prevent the default action list from showing
  • Prevent the browser from navigating to the URL with a long press.
  • Get the base href URL that the webview directly relates to, or the html tag itself and its associated attributes, such as an identifier.
+4
source share
2 answers

. 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
}
+1

( ) - presentViewController:animated:completion: .

Swift 3

override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {

  guard let alertController = viewControllerToPresent as? UIAlertController,
  alertController.preferredStyle == .actionSheet else {
    // Not an alert controller, present normally
    super.present(viewControllerToPresent, animated: flag, completion: completion)
    return    
  }

  // Create and open your own custom alert sheet
  let customAlertController = UIAlertController(...)
  super.present(customAlertController, animated: flag, completion: completion)
}

URL- alertController.title, , Firefox iOS, JS script, .

, , WKWebView, .

+2

Source: https://habr.com/ru/post/1619973/


All Articles