15

I'm migrating an iOS app form UIWebView to WKWebView. So far so good... In the previous app I disabled long press and implemented a custom long press (to do custom handling of links), however I can't get this working in the WKWebView

I've tried the following:

- (void)webView:(WKWebView *)wkWebView didFinishNavigation:(WKNavigation *)navigation {
    [wkWebView evaluateJavaScript:@"document.body.style.webkitTouchCallout='none';" completionHandler:nil];
}

I've checked and that line gets executed, the response of the call is @"None"

But it responds with: Warning: Attempt to present on whose view is not in the window hierarchy!

Any ideas? SOLUTION: Inject javascript into wkwebview now works!

[self.wkWebView evaluateJavaScript:@"document.body.style.webkitTouchCallout='none';" completionHandler:nil];
Daniel Åkesson
  • 1,330
  • 1
  • 12
  • 21

3 Answers3

18

This is the solution in Swift 3.0:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
     webView.evaluateJavaScript("document.body.style.webkitTouchCallout='none';")
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Sam
  • 283
  • 3
  • 12
14
myWkWebView.allowsLinkPreview = false
Valentin Shamardin
  • 3,569
  • 4
  • 34
  • 51
Eric Mentele
  • 864
  • 9
  • 16
  • Just what I've looked for! Thanks! On iOS 13 images in WKWebView become available to preview in a manner of 3D touch preview but even on non-3D touch capable devices. Just with long press. – HammerSlavik Mar 02 '20 at 17:09
  • This is perfect! It works just how I want it to. +1 – Amy Jul 31 '21 at 02:30
4

I haven't verified it, but it looks like you can also use CSS to disable this functionality:

-webkit-touch-callout: none;

Source: https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-touch-callout

Liron Yahdav
  • 10,152
  • 8
  • 68
  • 104
  • This is works for me iOS 12.4 and Swift 5 Work with this link https://stackoverflow.com/a/33126467/4593553 – Jerome Aug 18 '19 at 01:23