pyr*_*ion 24 javascript resources xcode mathjax ios
我需要从我的应用资源文件夹中加载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)
Nic*_*ver 50
在这里,我们进行简单的设置.
在Resources文件夹中创建以下文件夹结构.
请注意,蓝色文件夹是引用的文件夹

css只是糖果:)在lib.js中存在你想要使用的javascript代码.
的index.html
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/standard.css">
<script src="js/lib.js" type="text/javascript" />
</head>
<body>
<h2>Local Javascript</h2>
<a href="javascript:alert('Works!')">Test Javascript Alert</a>
<br/>
<br/>
<a href="javascript:alertMeWithMyCustomFunction('I am');">External js test</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
lib.js
function alertMeWithMyCustomFunction(text) {
alert(text+' -> in lib.js');
}
Run Code Online (Sandbox Code Playgroud)
在webview中加载内容
注意:webView是一个属性,使用实例构建器创建的视图
- (void)viewDidLoad
{
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:@"index"
ofType:@"html"
inDirectory:@"/htdocs" ];
NSString *html = [NSString stringWithContentsOfFile:htmlPath
encoding:NSUTF8StringEncoding
error:nil];
[webView loadHTMLString:html
baseURL:[NSURL fileURLWithPath:
[NSString stringWithFormat:@"%@/htdocs/",
[[NSBundle mainBundle] bundlePath]]]];
}
Run Code Online (Sandbox Code Playgroud)
这应该是结果:

编辑:
snowman4415提到iOS 7不喜欢自动关闭标签脚本标签,所以如果在iOS 7上无法正常工作,你可能需要关闭标签 </script>
TPo*_*hel 15
这是将本地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];
// ...
}
Run Code Online (Sandbox Code Playgroud)
当您不拥有正在显示的*.html/xhtml文件(即ePub阅读器或新闻聚合器)时,这尤其有用.它有助于您不必担心从xhtml文件到js文件的相对路径.
| 归档时间: |
|
| 查看次数: |
45888 次 |
| 最近记录: |