使用保存在iOS应用程序的沙箱(Documents目录)中的.html,.js文件,以便我能够以offilne模式打开html文件 [英] Using .html, .js file saved in the sandbox(Documents directory) of an iOS App so that I am able to open the html file in offilne mode as well

查看:165
本文介绍了使用保存在iOS应用程序的沙箱(Documents目录)中的.html,.js文件,以便我能够以offilne模式打开html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须显示一个需要highcharts.js,.jquery.min.js以便显示图形的.html文件。我打算在UIWebView中显示.html文件。



我看到,当文件(即html,highcharts.js,jquery.min)在应用程序中Bundle,我可以像这样使用它们 -



aPath = [[NSBundle mainBundle] pathForResource:@htmlfileofType:@html ]。
[self.graphWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:aPath isDirectory:NO]]];



htmlFile有这样的源代码 -

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title> TITLE< / title>

< script type =text / javascriptsrc =jquery.min.js>< / script>
< script type =text / javascriptsrc =highcharts.js>< / script>
< script type =text / javascriptsrc =htmlfile.js>< / script>
< script type =text / javascriptsrc =global.js>< / script>
< / head>
< body>
< div id =wrapper>

< div id =container>< / div>
< / div>
< / body>
< / html>

所以,为了在UIWebView中显示内容,我只需要制作.html文件和.js文件作为 Bundle Resources 包含在Xcode的Build Phases选项卡中。



问题:

b

当它保存在应用程序的沙盒中时,如何显示具有上述源代码的.html文件?



我尝试过:



1。我在App SandBox中创建了一个文件夹 -


NSArray * paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,YES);

NSString aFolderPath = [(NSString )[paths objectAtIndex:0] stringByAppendingFormat:@/%@,@folder];

<$如果(![[NSFileManager defaultManager] isWritableFileAtPath:aFolderPath]){

[[NSFileManager defaultManager] createDirectoryAtPath:aFolderPath withInt ermediateDirectories:NO attributes:nil error:nil];

2。然后我复制App SandBox中的html,js文件这样的应用程序包 -


NSString * aJs = [[NSBundle mainBundle] pathForResource:@JSFile
ofType: @js];

  aJs = [NSString stringWithContentsOfFile:aGlobaljs encoding:NSUTF8StringEncoding error:nil]; 

NSString * aPath = [(NSString *)[paths objectAtIndex:0] stringByAppendingFormat:@/ folder /%@,@JSFile.js];

[aJs writeToFile:aPath atomically:YES encoding:NSUTF8StringEncoding error:nil];


现在所有需要的文件都在名为文件夹,所以,我试图在UIWebView中打开保存的html文件 -


$ b


[self.webView loadRequest:[NSURLRequest requestWithURL :[NSURL
URLWithString:pathToHtmlFileinDocumentsDic relativeToURL:[NSURL
URLWithString:aFolderPath]]]];


<



我将非常感谢您提供的帮助。

解决方案

您必须将文件保存在应用程序资源中,这里的诀窍是保持JavaScript文件与HTML文件的相对链接。添加你的html包时,选择为所有添加的文件夹创建文件夹参考。并在代码中调用你的html页面:

  NSString * filePath = [[NSBundle mainBundle] pathForResource:@indexofType :@htmlinDirectory:@Help]; 

NSURL * indexUrl = [NSURL fileURLWithPath:filePath];

如果您的所有Html页面(包括 css images js )都在Help(或其他名称)文件夹中。即它像维护文件夹内的本地静态网站


I have to display an .html file which needs highcharts.js, jquery.min.js in order to display a graph. I am planning to display the .html file in a UIWebView.

I saw that when the files(i.e. html, highcharts.js, jquery.min) are in the Application Bundle, I can use them like this--

aPath = [[NSBundle mainBundle] pathForResource:@"htmlfile" ofType:@"html"]; [self.graphWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:aPath isDirectory:NO]]];

Here, the "htmlFile" has the source like this -

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TITLE</title>

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="highcharts.js"></script>
<script type="text/javascript" src="htmlfile.js"></script>
<script type="text/javascript" src="global.js"></script>
</head>
<body>
<div id="wrapper">

<div id="container"></div>    
</div>
</body>
</html>

So, in order to display the content in UIWebView, i just needed to make the .html file and .js files as Bundle Resources in Xcode's Build Phases Tab.

Problem:

How do display the .html file having the source like shown above when It is saved in the App's Sand Box?

Wat I tried:

1. I created a folder in the App SandBox like this--

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ;

NSString aFolderPath = [(NSString)[paths objectAtIndex:0] stringByAppendingFormat:@"/%@",@"folder"];

if(![[NSFileManager defaultManager] isWritableFileAtPath:aFolderPath]){

[[NSFileManager defaultManager]createDirectoryAtPath:aFolderPath withIntermediateDirectories:NO attributes:nil error:nil]; }

2. Then I copied the html, js files in the App SandBox from the App Bundle like this--

NSString *aJs = [[NSBundle mainBundle] pathForResource:@"JSFile" ofType:@"js"];

aJs = [NSString stringWithContentsOfFile:aGlobaljs encoding:NSUTF8StringEncoding error:nil];

NSString *aPath = [(NSString*)[paths objectAtIndex:0] stringByAppendingFormat:@"/folder/%@",@"JSFile.js"];

[aJs writeToFile:aPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

Now all the needed files are in the SandBox Folder named "folder", so, I tried to open the saved html file in the UIWebView like this--

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:pathToHtmlFileinDocumentsDic relativeToURL:[NSURL URLWithString:aFolderPath]]]];

This does not work.

I will be grateful for some help provided.

解决方案

You will have to keep your files in your apps resources, the trick here is to maintain the relative linking of the JavaScript files with the HTML files. When adding your html package select "Create folder reference for any added folders". and call your html page like this in code:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"Help"];

  NSURL *indexUrl = [NSURL fileURLWithPath:filePath];

Where all your Html pages (including css, images, js) are in Help (or any other name you have) folder. i.e., its like maintaining a local static site inside the folder

这篇关于使用保存在iOS应用程序的沙箱(Documents目录)中的.html,.js文件,以便我能够以offilne模式打开html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆