为了账号安全,请及时绑定邮箱和手机立即绑定

从资源将JavaScript加载到UIWebView中

从资源将JavaScript加载到UIWebView中

慕的地8271018 2019-11-28 15:09:00
我需要从我的应用程序资源文件夹中加载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]];
查看完整描述

3 回答

?
白衣非少年

TA贡献1155条经验 获得超0个赞

这是将本地javascript文件注入到Web视图的DOM中的另一种方法。您将JS文件的内容加载到字符串中,然后使用stringByEvalutatingJavaScriptFromString:


- (void)webViewDidFinishLoad:(UIWebView *)webView {

    NSString *jsFile = @"jquery-1.8.2.min.js";

    NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:jsFile ofType:nil];

    NSURL *jsURL = [NSURL fileURLWithPath:jsFilePath];

    NSString *javascriptCode = [NSString stringWithContentsOfFile:jsURL.path encoding:NSUTF8StringEncoding error:nil];

    [webView stringByEvaluatingJavaScriptFromString:javascriptCode];

    // ...

}

如果您不拥有要显示的* .html / xhtml文件(例如ePub阅读器或新闻聚合器),则此功能特别有用。它可以帮助您不必担心从xhtml文件到js文件的相对路径。


查看完整回答
反对 回复 2019-11-28
?
心有法竹

TA贡献1866条经验 获得超5个赞

这就是我使用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(){


    if let resourceUrl = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "WebDocs2"){

        let urlRequest = URLRequest.init(url: resourceUrl)

        myWebView.loadRequest(urlRequest)

    }

}

// Load the JS from resources

func jsScriptText() -> String? {


    guard let jsPath = Bundle.main.path(forResource: "hello", ofType: "js", inDirectory: "WebDocs2/scripts") else {


        return nil

    }

    do

    {

        let jsScript = try String(contentsOfFile: jsPath, encoding: String.Encoding.utf8)

        return jsScript

    }catch{


        print("Error")

        return nil

    }


}

// Run the java script

func runJS(){


    if let jsScript = jsScriptText(){


        let jsContext = JSContext()

        _ = jsContext?.evaluateScript(jsScript)

        let helloJSCore = jsContext?.objectForKeyedSubscript("helloJSCore")

        let result = helloJSCore?.call(withArguments: [])

        print(result?.toString() ?? "Error")


    }

}


查看完整回答
反对 回复 2019-11-28
  • 3 回答
  • 0 关注
  • 416 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号