将 JavaScript 注入 UIWebView
我正在尝试使用 stringByEvaluatingJavaScriptFromString:
来更改 WebView,但它没有按我的预期执行。
这是代码,但是这个 JS 并没有执行。知道为什么吗?
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')"];
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')"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能必须等待请求完成才能使用
stringByEvaluatingJavaScriptFromString
。设置wv.delegate = self;
然后像这样实现-webViewDidFinishLoad
Probably you'll have to wait for the request to finish before you can use
stringByEvaluatingJavaScriptFromString
. Setwv.delegate = self;
and then implement-webViewDidFinishLoad
like this