1

I am trying to use the stringByEvaluatingJavaScriptFromString: to alter a WebView and it's not executing as I expect.

Here is the code, this JS doesn't execute though. Any idea why?

UIWebView *wv = [UIWebView alloc] init];

NSURL *url = [[NSURL alloc] initWithString:@"http://www.google.com"];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

[wv loadRequest:request];

[wv stringByEvaluatingJavaScriptFromString:@"document.write('This Works')"];

1 Answer 1

3

Probably you'll have to wait for the request to finish before you can use stringByEvaluatingJavaScriptFromString. Set wv.delegate = self; and then implement -webViewDidFinishLoad like this

-(void)webViewDidFinishLoad:(UIWebView *)webView {
    [webView stringByEvaluatingJavaScriptFromString:@"document.write('This Works')"];
}
Sign up to request clarification or add additional context in comments.

5 Comments

After I try this, the WebView goes blank and I can't see "This Works" anywhere.
@Andrew Barinov, If you need to stop it, use a boolean flag. If you want to write a text to google search field, document.write is not what you are looking for.
That's strange, it works for me. Your delegate methods are getting called properly, right?
Yep, I put an NSLog into my delegate method and I see it printing on my console.
@user792677 I'm actually trying to modify the structure of the page the user sees, not perform a search.

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.