How can I inject custom CSS into a WebView like changing the background-color: of http://www.apple.com/. Javascript would also be nice because in the future I would like to have control of the javascript.
1 Answer
The best way would be the one delineated by Rob Keniger, with this answer: https://stackoverflow.com/a/2475623/307881. This technique should also work for javascript. If it doesn't work off the bat try modifying the DOM after the WebView has finished loading (see below)
You can also directly evaluate a javascript string without accessing the DOM.
First, get your javascript string, then, when your WebView finishes loading its content, use the delegate method of WebFrameLoadDelegate:
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
[sender stringByEvaluatingJavaScriptFromString:jsStringToInject];
}
7 Comments
atomikpanda
the CSS part woks but not the JS
Vervious
Did you try using stringByEvaluatingJavaScriptFromString? Or do both methods not work?
Vervious
Do you get any errors? I currently use the second method and it works well - what javascript are you trying to embed?
atomikpanda
I don't get any errors i'm just trying to test it using the js:
alert('Hello World!');Vervious
Well, alert() has never worked for me in a WebView. Maybe stringByEvaluatingString doesn't support it.
|