Reputation: 1315
In my view controller, I have a WKWebView
and I use a UITapGestureRecognizer
to detect taps on the webview in order to show/hide the navigation and status bars. I also use the taps+javascript to detect when special parts of the web content are tapped.
WKWebView uses long-presses to allow the user to select text.
Super-long-presses work great. The web content is selected by WKWebView as expected and life is good.
My problem is when the user applies "shorter"-long-presses, because the UITapGestureRecognizer recognizes them as taps. WKWebView selects the text, so it seems like the user pressed long enough, but upon release the UITapGestureRecognizer fires the designated action and my code to show/hide the navigation bar kicks in.
What I want is to only show/hide the navigation bar when the user applies a very short tap. If they touch long enough for WKWebView to select text, I want to let WKWebView handle it.
I was hoping to filter taps by touch duration, but I haven't been able to find a way to determine this information.
Am I missing something? Can I achieve this with a UITapGestureRecognizer or do I need a different approach?
I'm targetting iOS 8 & 9.
Upvotes: 2
Views: 1023
Reputation: 1198
If @melany code does't work properly then make following changes ...
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
switch navigationAction.navigationType {
case .linkActivated:
UIApplication.shared.openURL(navigationAction.request.url!)
decisionHandler(.cancel)
return
default:
break
}
decisionHandler(.allow)
}
Upvotes: 0
Reputation: 466
For WKWebView the most simple solution will be just to use method from WKNavigationDelegate protocol and inside of it decide whether to handle that url or not.
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.url, url.scheme?.lowercased() == Constants.mailToScheme {
UIApplication.shared.open(url, options: [:])
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
}
Upvotes: 1
Reputation: 10821
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:
UILongPressGestureRecognizer
. This long press recognizer doesn't do anything on its own, but it's needed by step 2.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.
Upvotes: 4