Prevent iOS Webkit Long Press on Link

2020-05-19 05:58发布

In an iOS app I'm developing, I show an UIWebView to allow the user to upgrade their account. The page contains links to show the user other informations, but you hold your finger on the link, a menu like this will pop up: enter image description here

As you can see, it reveals the URL of the page and the action that will be performed, which I do not want the user to see, since I don't want them to be able to copy anything as I disabled the "Copy/Cut/Paste" menu already. How would I go about disabling this menu as well?

Thanks for any pointers.

4条回答
欢心
2楼-- · 2020-05-19 06:33

No need to tell the browser you have a link and then force it not to behave like one!

Just remove the href="void(0)" completely!

Your ontouchend or onclick attributes will still work.


If you want the normal blue/underlined look again, you can add this css:

a .originalLink { color: blue; text-decoration: underline; cursor: pointer; }

This saves a lot of CPU cycles, especially when there are hundreds of links in the page while the user is smooth scrolling.

You can also extend this to regular URLs by using this:

<a class="originalLink" onclick="location.href='http://mylink';">Real URL Link</a>

Of course your SEO is out the window, but for a specific mobile web app situation this can be crucial.

Happy coding!

查看更多
叼着烟拽天下
3楼-- · 2020-05-19 06:36

I think you can disable that popup by setting the CSS -webkit-touch-callout property to none on the link. You would do this by editing the HTML or CSS file you're loading, not using Objective-C.

查看更多
手持菜刀,她持情操
4楼-- · 2020-05-19 06:44

If you want to both disable the popup AND user selection of text on your ENTIRE HTML PAGE, use this on the BODY of your web page or global css.

  body {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
查看更多
Root(大扎)
5楼-- · 2020-05-19 06:49

What you want to do, as Rob already stated, is to disable the default contextual menu of UIWebView. You can achieve it by adding the following line to webViewDidFinishLoad:

   [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';"];
查看更多
登录 后发表回答