从资源将JavaScript加载到UIWebView中

我需要从我的应用程序资源文件夹中加载javascript文件。截至目前,它确实可以从资源中读取图像,但是由于某种原因它并未读取javascript。


如果将html文件本身写入资源中,我已经能够呈现引用这些javascript的html文档,但是我想通过设置UIWebView的html来呈现html / js,以便它可以是动态的。


这是我在做什么:


NSString * html = @"<!DOCTYPE html><html><head><title>MathJax</title></head><body><script type=\"text/x-mathjax-config\">MathJax.Hub.Config({tex2jax: {inlineMath: [[\"$\",\"$\"],[\"\\(\",\"\\)\"]]}});</script><script type=\"text/javascript\" src=\"/MathJax/MathJax.js\"></script>$$\\int_x^y f(x) dx$$<img src=\"coffee.png\"></body></html>";


NSString * path = [[NSBundle mainBundle] resourcePath]; 

path = [path stringByReplacingOccurrencesOfString:@"/" withString:@"//"];

path = [path stringByReplacingOccurrencesOfString:@" " withString:@"%20"];


NSString * resourcesPath = [[NSString alloc] initWithFormat:@"file://%@/", path];

[webview loadHTMLString:html baseURL:[NSURL URLWithString:resourcesPath]];

现在,如果我将基本URL更改为服务器上也包含必需文件的服务器,则它会正确加载。不需要互联网连接就很好。任何帮助表示赞赏!!!;)


我发现这对显示图像很有用:iPhone Dev:UIWebView baseUrl到Documents文件夹中的资源,而不是App bundle


编辑:


无需进行字符串替换和URL编码,我仅通过在mainBundle上调用resourceURL即可获取图像,但仍无法执行JavaScript。


    NSString * setHtml = @"<!DOCTYPE html><html><head><title>MathJax</title></head><body><script type=\"text/x-mathjax-config\">MathJax.Hub.Config({tex2jax: {inlineMath: [[\"$\",\"$\"],[\"\\(\",\"\\)\"]]}});</script><script type=\"text/javascript\" src=\"/MathJax/MathJax.js\"></script>$$\\int_x^y f(x) dx$$<img src=\"images/test.png\"></body></html>";

    [webview loadHTMLString:setHtml baseURL:[[NSBundle mainBundle] resourceURL]];


慕的地8271018
浏览 417回答 3
3回答

白衣非少年

这是将本地javascript文件注入到Web视图的DOM中的另一种方法。您将JS文件的内容加载到字符串中,然后使用stringByEvalutatingJavaScriptFromString:- (void)webViewDidFinishLoad:(UIWebView *)webView {&nbsp; &nbsp; NSString *jsFile = @"jquery-1.8.2.min.js";&nbsp; &nbsp; NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:jsFile ofType:nil];&nbsp; &nbsp; NSURL *jsURL = [NSURL fileURLWithPath:jsFilePath];&nbsp; &nbsp; NSString *javascriptCode = [NSString stringWithContentsOfFile:jsURL.path encoding:NSUTF8StringEncoding error:nil];&nbsp; &nbsp; [webView stringByEvaluatingJavaScriptFromString:javascriptCode];&nbsp; &nbsp; // ...}如果您不拥有要显示的* .html / xhtml文件(例如ePub阅读器或新闻聚合器),则此功能特别有用。它可以帮助您不必担心从xhtml文件到js文件的相对路径。

心有法竹

这就是我使用Webview和本地JS的方式。把一些快照这里的示例项目在这里// Get the path for index.html, where in which index.html has reference to the js files, since we are loading from the proper resource path, the JS files also gets picked up properly from the resource path.func loadWebView(){&nbsp; &nbsp; if let resourceUrl = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "WebDocs2"){&nbsp; &nbsp; &nbsp; &nbsp; let urlRequest = URLRequest.init(url: resourceUrl)&nbsp; &nbsp; &nbsp; &nbsp; myWebView.loadRequest(urlRequest)&nbsp; &nbsp; }}// Load the JS from resourcesfunc jsScriptText() -> String? {&nbsp; &nbsp; guard let jsPath = Bundle.main.path(forResource: "hello", ofType: "js", inDirectory: "WebDocs2/scripts") else {&nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; }&nbsp; &nbsp; do&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; let jsScript = try String(contentsOfFile: jsPath, encoding: String.Encoding.utf8)&nbsp; &nbsp; &nbsp; &nbsp; return jsScript&nbsp; &nbsp; }catch{&nbsp; &nbsp; &nbsp; &nbsp; print("Error")&nbsp; &nbsp; &nbsp; &nbsp; return nil&nbsp; &nbsp; }}// Run the java scriptfunc runJS(){&nbsp; &nbsp; if let jsScript = jsScriptText(){&nbsp; &nbsp; &nbsp; &nbsp; let jsContext = JSContext()&nbsp; &nbsp; &nbsp; &nbsp; _ = jsContext?.evaluateScript(jsScript)&nbsp; &nbsp; &nbsp; &nbsp; let helloJSCore = jsContext?.objectForKeyedSubscript("helloJSCore")&nbsp; &nbsp; &nbsp; &nbsp; let result = helloJSCore?.call(withArguments: [])&nbsp; &nbsp; &nbsp; &nbsp; print(result?.toString() ?? "Error")&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript