1

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.

0

1 Answer 1

2

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];
}
Sign up to request clarification or add additional context in comments.

7 Comments

the CSS part woks but not the JS
Did you try using stringByEvaluatingJavaScriptFromString? Or do both methods not work?
Do you get any errors? I currently use the second method and it works well - what javascript are you trying to embed?
I don't get any errors i'm just trying to test it using the js: alert('Hello World!');
Well, alert() has never worked for me in a WebView. Maybe stringByEvaluatingString doesn't support it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.