UIWebView Disable long press the popup copy selection box

Source: Internet
Author: User

This article refer to http://bbs.9ria.com/thread-227539-1-1.html thank Hbnana great god

The first type of loading HTML file

Add the code to the WebView loading agent method.

Prohibit long press the address of the pop-up image, etc.

[self. WebView stringbyevaluatingjavascriptfromstring:@ "document.body.style.webkittouchcallout= ' None ‘;" ];

Disable Long Press Copy

[self. WebView stringbyevaluatingjavascriptfromstring:@ " Document.documentelement.style.webkituserselect= ' None '; ];

The second type of loading PDF file

Second case: If UIWebView loads a PDF document, see the
Loading PDFs with UIWebView should be the most convenient way to browse PDFs in iOS.
However, it may be possible for customers to consider safety issues and to prohibit the opening of the webview above
Long press the feature that appears to copy text or pictures. My last project client had this requirement,
To achieve this, I did study for a while. And finally found a very simple implementation
method, just add a few more lines of code.
Today to share with you, can give some people who are struggling with this problem to provide some help so the best,
If there is a better way to realize this feature, you are welcome to teach more ....

  1. NSString *path = [[NSBundle mainbundle] pathforresource:@ "sample" oftype:@ "PDF"];
  2. nsurlrequest* pdfrequest = [nsurlrequest requestwithurl:[nsurl Fileurlwithpath:path];
  3. [Self.pdfview Loadrequest:pdfrequest];
  4. Create a custom long-press gesture in viewdidload or where appropriate
  5. Uilongpressgesturerecognizer *longpress = [[[Uilongpressgesturerecognizer alloc] initwithtarget:self Action:nil] Autorelease];
  6. Longpress.delegate = self; Remember to add <UIGestureRecognizerDelegate> delegates to the. h file
  7. Longpress.minimumpressduration = 0.4; Why set 0.4 here, because as long as the greater than 0.5 is invalid, I like probably because the default step out of the magnifying glass gesture of the long-time is 0.5 seconds,
  8. If our custom gesture is greater than or less than 0.5 seconds, it's too late to replace his default gesture, which is just my guess. But better than 0.2.
  9. Seconds, because some PDFs have a bookmark jump function, which is too small to invalidate these features.
  10. [Self.pdfview addgesturerecognizer:longpress];
  11. The next step is to implement a delegate.
  12. #pragma mark-gesturerecognizerdelegate
  13. -(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer Shouldrecognizesimultaneouslywithgesturerecognizer: (Uigesturerecognizer *) Othergesturerecognizer
  14. {
  15. return NO; Be sure to return NO here, as to why everyone should take a look at the documentation for this method.
  16. There is this delegate will be called multiple times when you press the NSLog, you can use the output Gesturerecognizer and Othergesturerecognizer
  17. Let's see what it is.
  18. }

UIWebView Disable long press the popup copy selection box

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.