将本地 javascript 注入 UIWebView [英] Inject a local javascript into UIWebView

查看:15
本文介绍了将本地 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆