Wkwebview Decidepolicyfornavigationaction Being Call After the Long Press Recogniser Ended

ios8 gesture recognizer does not work on WKWebView with swift

You didn't set the delegate of the gesture recognizer.

//hook the long press event
longPressRecognizer.delegate = self
longPressRecognizer.addTarget(self, action: "onLongPress:")
self.webView!.scrollView.addGestureRecognizer(longPressRecognizer)

In case that it still doesn't work, this may probably due to WKWebView already has its own gesture recognizers. Then add the following method to your class:

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

And in your event method check for the gesture began:

func onLongPress(gestureRecognizer:UIGestureRecognizer){
if gestureRecognizer.state == UIGestureRecognizerState.Began {
NSLog("long press detected")
}
}

How to override wkwebview hyperlink action sheet

Here's a working solution for overriding the menu. I would still like to get some html attributes, but don't know how to do that yet.

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
}

How to detect only short taps

If you have access to the gesture recognizer used by the WKWebView that selects the text, then you can setup your short tap recognizer to require that WKWebView recognizer to fail, using requireGestureRecognizerToFail:.

If you don't have access to the gesture recognizer used by the WKWebView, then you can take this hackier approach:

  1. Create a UILongPressGestureRecognizer. This long press recognizer doesn't do anything on its own, but it's needed by step 2.
  2. In your short tap recognizer, use requireGestureRecognizerToFail:, specifying the UILongPressGestureRecognizer from step 1.

Note, this second approach only has a shot of working if WKWebView is using a UILongPressGestureRecognizer under the hood.

Open popup on long press gesture like Whats app

You can do something like this:

 -(void) handleLongPress:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
{
//Start a timer and perform action after whatever time interval you want.
}
if (sender.state == UIGestureRecognizerStateEnded)
{
//Check the duration and if it is less than what you wanted, invalidate the timer.
}
}


Related Topics



Leave a reply



Submit