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?
假设我在该 UIWebView 中导航到 google.com,它会呈现该页面并运行我的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将本地 javascript 注入 UIWebView


- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- UITextView 内容插图 2022-01-01
- URL编码Swift iOS 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01