There appear to be several suggestions, but nothing seems to work:
https://stackoverflow.com/search?q=UIWebview+load+local+css+remote+website&submit=search
What I am doing is loading 2 JavaScript files after a website has loaded.
The first one sets some properties and the second one goes through the DOM and alters the page formatting. I can verify that the link tag with the correct href is inserted into the DOM by looping through the link elements. The processing done by the 2nd script also runs correctly.
I can also set this up as a bookmarklet in a browser and this works on all browsers I’ve tested so far. The CSS only doesn’t take in a UIWebView.
Has anyone to got this to work recently?
Obj-C
NSString * css = [[NSBundle mainBundle] pathForResource:@"mycss" ofType:@"css"];
NSURL * cssURL = [NSURL fileURLWithPath:css];
[_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"appendCSS('%@')", cssURL]];
JavaScript
function appendCSS(csshref)
{
//alert(css);
var css=document.createElement('link');
css.rel='stylesheet';
css.href=csshref;
css.type='text/css';
css.media='all';
//Try a couple of things. Appear to be equivalent.
//document.documentElement.appendChild(css);
document.getElementsByTagName('head')[0].appendChild(css);
}
A remote webpage can’t access local files. (The URL you’re using is to a file in your bundle.)
Can you host the CSS elsewhere? Otherwise, you might just load the CSS from the file yourself and insert it into the DOM in a style element.