Showing posts with label javaScript. Show all posts
Showing posts with label javaScript. Show all posts

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:
   @"<html><head>"
   "<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;"
   "}"
   ",</style>"
   "</head><body>%@</body></html>", message.body];
[myWebView loadHTMLString:myHtml baseURL:nil];
As a bonus I left there also how to disable link selection popup.

Wednesday, February 10, 2010

How to Make UIWebView Open target="_blank" Links

There are several ways to do this, here's mine.
NSString *js = @"\
var a = document.getElementsByTagName('a');\
for (var i=0; i<a.length; i++) {\
    a[i].removeAttribute('target');\
}\
";
[self.myWebView stringByEvaluatingJavaScriptFromString:js];
Possible advantage: reduces size of HTML document.