相关疑难解决方法(0)

从资源将javascript加载到UIWebView中

我需要从我的应用资源文件夹中加载javascript文件.截至目前,它确实从资源中读取图像,但由于某种原因,它不会读取javascripts.

如果html文件本身被写入资源,我已经能够呈现引用这些javascripts的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]];
Run Code Online (Sandbox Code Playgroud)

现在,如果我将基本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]];
Run Code Online (Sandbox Code Playgroud)

编辑

如果你想帮助我做一个镜头,我通过创建一个示例项目让你更容易!

https://github.com/pyramation/math-test

git clone git@github.com:pyramation/math-test.git
Run Code Online (Sandbox Code Playgroud)

javascript resources xcode mathjax ios

24
推荐指数
2
解决办法
5万
查看次数

从设备本地文件系统响应本机webview负载

我试图加载.html,.js,.css从设备的本地文件系统中的文件到本机作出反应的WebView组件.我能够获取index.html文件的路径,但将其指定为WebView 的url只会引发错误.我该怎么做?请帮忙!

webview react-native

18
推荐指数
4
解决办法
2万
查看次数

标签 统计

ios ×1

javascript ×1

mathjax ×1

react-native ×1

resources ×1

webview ×1

xcode ×1