Thursday, February 25, 2010

How to Disable UIWebView Selection

You could do this by running a short javaScript line at webViewDidFinishLoad, but I find this more elegant: define UIWebView properties in CSS.
NSString *myHtml = [NSString stringWithFormat:
   "<style type=\"text/css\">"
   // Disable Open/Copy/Cancel alert with long link selection
   "* {"
   "-webkit-touch-callout: none;"
   // Disable selection/Copy of UIWebView
   "-webkit-user-select: none;"
   "</head><body>%@</body></html>", message.body];
[myWebView loadHTMLString:myHtml baseURL:nil];
As a bonus I left there also how to disable link selection popup.


  1. Thanks! Was trying to find a way to do exactly this, and it works perfectly. :)

  2. Need Help !! , how do i enable copy on the UIwebview ,As I am not getting the copy option by holding the tap with a PDF Opened in the UIWebview, any kinda help would be highly appreciated , thanks

  3. Coincident, but one of the apps I'm doing now shows PDF files inside UIWebView. It doesn't offer text selection either, didn't have to disable it.

    Guess "PDF inside UIWebView" does not support copy & paste. If you want to have full control over PDF content, check SDK manual for "PDF". There are lots of lower level routines to do a lot of things. Might get complicated, though. Good luck!

  4. Thank you very much for this post. It worked for me.

  5. After adding it i am not able to scroll through the page it is calling - (void)webViewDidFinishLoad:(UIWebView *)webView again and again

  6. Don't think CSS could make your app to call webViewDidFinishLoad in a loop... However I've seen that with complex webpages containing lots of e.g. images. All are loaded separately and each will call webViewDidFinishLoading. Your task is to keep track of when "original" document load is completed. Btw tip: test your code with simple text-only webpage to make sure it works. Start simple, go complex after that.

  7. It is working till iOS 4.2 but not on 4.3
    Any way to do it in 4.3?

  8. I am using the same in my app, but I am not getting what is massage.body, If anybody has an idea please let me know

  9. message.body is a local variable containing whatever text I want to display on that dynamically created web page. You can replace it with e.g. @"Sample text".