从资源将JavaScript加载到UIWebView中

前端之家收集整理的这篇文章主要介绍了从资源将JavaScript加载到UIWebView中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要从我的应用程序资源文件夹加载javascript文件。到目前为止,它从资源中读取图像很好,但是由于某种原因它不会读取javascripts。

如果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更改为我的服务器也有所需的文件,它正确加载。这不是一个互联网连接是非常好的。任何帮助是赞赏!

猜你在找的Xcode相关文章