注入JavaScript到UIWebView [英] Injecting JavaScript into UIWebView
本文介绍了注入JavaScript到UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图使用 stringByEvaluatingJavaScriptFromString:
来修改一个WebView,它没有按预期执行。
I am trying to use the stringByEvaluatingJavaScriptFromString:
to alter a WebView and it's not executing as I expect.
这里是代码,这个JS不执行。任何想法为什么?
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')"];
推荐答案
可能需要等待请求完成后,您可以使用 stringByEvaluatingJavaScriptFromString
。设置 wv.delegate = self;
然后实现 -webViewDidFinishLoad
像这样
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')"];
}
这篇关于注入JavaScript到UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文