Injecting CSS into UIWebView using JavaScript?

Up vote 0 down vote favorite share g+ share fb share tw.

I am attempting to inject a local CSS file to override the styling of a webpage. The webpage is presented in a UIWebView container in iOS. However I am not able to get my code to work.

See the snippet of my delegate method below. This code runs (I can see the NSLog message) but I do not see the results of it's execution on the page. I know it can't be the CSS I wrote because in this case I took the pages own CSS file and simply changed some colors.

(In order to test this method) -(void)webViewDidFinishLoad:(UIWebView *)webView { NSString *path = NSBundle mainBundle bundlePath; NSString *cssPath = path stringByAppendingPathComponent:@"reader. Css"; NSString *js = NSString stringWithFormat:@"var headID = document. GetElementsByTagName('head')0;var cssNode = document.

CreateElement('link');cssNode. Type = 'text/css';cssNode. Rel = 'stylesheet';cssNode.

Href = '%@';cssNode. Media = 'screen';headID. AppendChild(cssNode);", cssPath; webView stringByEvaluatingJavaScriptFromString:js; NSLog(@"webViewDidFinishLoad Executed"); } javascript objective-c ios uiwebview javascript-injection link|improve this question asked Jan 9 at 19:04Andrew Barinov35218 94% accept rate.

Your solution won't work because your cssNode. Href should be a URL (i.e. Escaped and prefixed with file://), not a path Safari doesn't let you load local files from a remote page, as it's a security risk.

In the past I've done this by downloading the HTML using an NSURLConnection, and then adding a ", iOSCssData; html = uneditedHtml stringByReplacingOccurrencesOfString:@"" withString:extraHeadTags; webView loadHTMLString:html baseURL:url.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions