Xcode – Inserting CSS & Javascript into a UIWebView

Written by Karl Binder on January 26, 2012

Whilst creating an iPhone application recently we had a need to be able to insert out own custom stylesheet and javascript, or jquery into certain web pages loaded within a UIWebView within the application.

We did it in the webViewDidFinishLoad section of the ViewController.m file:

[CSSWebView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js';"


[CSSWebView stringByEvaluatingJavaScriptFromString:@"var link = document.createElement('link');"
"link.type = 'text/css';"
"link.rel = 'stylesheet';"
"link.href = 'http://[your url goes in here]/style.css';"