将本地javascript注入UIWebView [英] Inject a local javascript into UIWebView
本文介绍了将本地javascript注入UIWebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法将存储在我服务器上的本地JS文件或JS文件注入UIWebView加载的任何网页?
Is there a way to inject a local JS file or a JS file that's store on my server into any web page loaded by UIWebView?
说我导航到谷歌.com在UIWebView中,它呈现该页面并运行我的JS文件(本地或我的网络服务器)?
Say I navigate to google.com in that UIWebView, it renders that page and also runs my JS file (local or on my webserver)?
推荐答案
以下代码将在本地注入javascript
The following code will inject javascript locally
- (void)injectJavascript:(NSString *)resource {
NSString *jsPath = [[NSBundle mainBundle] pathForResource:resource ofType:@"js"];
NSString *js = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:NULL];
[self.webView stringByEvaluatingJavaScriptFromString:js];
}
只需在js之前传入文件名,例如
Just pass in the name of the file before the js, eg.
[self injectJavascript:@"script"];
要从您的服务器注入javascript,您可以先下载它并将其加载到Web视图中类似的方式。
To do inject javascript from your server you can download it first and load it into the web view in a similar manner.
这篇关于将本地javascript注入UIWebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文