Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

javascript - 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 offline mode as well

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...