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
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
