admin管理员组文章数量:1299986
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");
}
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");
}
Share
Improve this question
asked Jan 9, 2012 at 19:04
Andrew Lauer BarinovAndrew Lauer Barinov
5,75410 gold badges61 silver badges86 bronze badges
1 Answer
Reset to default 9Your solution won't work because
- your
cssNode.href
should be a URL (i.e. escaped and prefixed withfile://
), 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 <style>
tag in the HTML head. Something like:
NSString *pathToiOSCss = [[NSBundle mainBundle] pathForResource:@"reader" ofType:@"css"];
NSString *iOSCssData = [NSString stringWithContentsOfFile:pathToiOSCss encoding:NSUTF8StringEncoding error:NULL];
NSString *extraHeadTags = [NSString stringWithFormat:@"<style>%@</style></head>", iOSCssData];
html = [uneditedHtml stringByReplacingOccurrencesOfString:@"</head>" withString:extraHeadTags];
[webView loadHTMLString:html baseURL:url];
本文标签: objective cInjecting CSS into UIWebView using JavaScriptStack Overflow
版权声明:本文标题:objective c - Injecting CSS into UIWebView using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741654379a2390669.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论