I tried to override the original wkwebview actionsheet...
Firstly I disabled the original wkactionsheet by webView.evaluateJavaScript("document.body.style.webkitTouchCallout='none';", completionHandler: nil)
Then I initialise a long press gesture recogniser (and it works perfectly) and I created my own action sheet. I used decidePolicyForNavigationAction to get the clicked link urls:
func onLongPress(gestureRecognizer:UIGestureRecognizer){
if gestureRecognizer.state == UIGestureRecognizerState.Began {
longPressSwitch = true
}
}
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
if(navigationAction.navigationType == .LinkActivated) {
longPressAcUrl = navigationAction.request.URL!.absoluteString
if(longPressSwitch == true) {
let ac = actionMenu(self)
self.presentViewController(ac, animated: true) {
}
decisionHandler(.Cancel)
longPressSwitch = false
}
}
decisionHandler(.Allow)
}
The problem is, the action sheet shows up after the finger is releasing (i.e., recogniser.state = .Ended) but I want it shows up like Chrome, which should be 0.5 or less seconds after the user pressing the link... (i.e., recogniser.state = .Begin), what can I do?
ps: this is my action sheet:
//Rebuild Wkactionsheet
func actionMenu(sender: UIViewController) -> UIAlertController {
let alertController = UIAlertController(title: "", message: longPressAcUrl, preferredStyle: .ActionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
}
alertController.addAction(cancelAction)
let openAction = UIAlertAction(title: "Open", style: .Default) { (action) in
//...
}
alertController.addAction(openAction)
let opentabAction = UIAlertAction(title: "Open in New Tab", style: .Default) { (action) in
//...
}
alertController.addAction(opentabAction)
let copyurlAction = UIAlertAction(title: "Copy Link URL", style: .Default) { (action) in
//...
}
alertController.addAction(copyurlAction)
return alertController
}
also, if I tried to put
let ac = actionMenu(self)
self.presentViewController(ac, animated: true) {}
at onLongPress(), it works fine although this can't get URL (longPressAcUrl) from navigationAction.request.URL!.absoluteString!