Injecting JavaScript into UIWebView

怎甘沉沦 提交于 2019-12-08 09:55:02

问题


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:


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')"];
}


来源:https://stackoverflow.com/questions/8786690/injecting-javascript-into-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!