我需要从我的应用程序资源文件夹中加载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]];
白衣非少年
心有法竹
相关分类