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