Disable WKActionSheet on WKWebView

2020-02-26 08:46发布

问题:

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];

回答1:

This is the solution in Swift 3.0:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
     webView.evaluateJavaScript("document.body.style.webkitTouchCallout='none';")
}


回答2:

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



回答3:

myWkWebView.allowsLinkPreview = false