Disable WKActionSheet on WKWebView
Asked Answered
N

3

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];
Nunatak answered 20/10, 2014 at 7:26 Comment(10)
Bug reported: devforums.apple.com/message/1024729#1024729Fudge
You stated in your "edit" you have solved it. Would be interesting to know how, it was not clear for me. Any hints?Uptodate
Sorry for bad description but wasn't sure what really happened.Fudge
I really need an answer to this... How? Did Apple approve?Holdall
Not sure what happened, but apple did approveFudge
Can you explain how did you solve this problem. thx. All I am trying to have cut.copy, paste menu on long pressVeliz
@DanielÅkesson can you provide an answer for how to present a custom hyperlink handler for a wkwebview with associated custom action sheet? I would love to figure it out and upvote the answer.Magma
Hi Daniel, as asked above by other people, could you please provide the solution you've found ? It looks like you don't want to share it; it's kind of weird to ask for some help to a community but not be willing to help that community back.Lamarckism
The javascript injection works for me in later versions. I updated the solutionFudge
does this work on latest iOS versions?Wanda
T
18

This is the solution in Swift 3.0:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
     webView.evaluateJavaScript("document.body.style.webkitTouchCallout='none';")
}
Tautog answered 30/9, 2017 at 19:59 Comment(0)
S
14
myWkWebView.allowsLinkPreview = false
Schuck answered 19/12, 2019 at 20:9 Comment(2)
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.Flannelette
This is perfect! It works just how I want it to. +1Heterotrophic
S
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

Spelldown answered 17/4, 2018 at 18:32 Comment(1)
This is works for me iOS 12.4 and Swift 5 Work with this link https://mcmap.net/q/360668/-insert-css-into-loaded-html-in-uiwebview-wkwebviewSculley

© 2022 - 2024 — McMap. All rights reserved.