グラフを表示するには、highcharts.js、jquery.min.js を必要とする .html ファイルを表示する必要があります。.html ファイルを UIWebView に表示する予定です。
ファイル (つまり、html、highcharts.js、jquery.min) がアプリケーション バンドルにある場合、次のように使用できることがわかりました--
aPath = [[NSBundle mainBundle] pathForResource:@"htmlfile" ofType:@"html"];
[self.graphWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:aPath isDirectory:NO]]];
ここで、「htmlFile」には次のようなソースがあります-
<!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>
したがって、コンテンツを UIWebView に表示するには、.html ファイルと .js ファイルをXcode の Build Phases タブでBundle Resourcesとして作成するだけで済みました。
問題:
上記のようなソースを持つ .html ファイルをアプリのサンド ボックスに保存すると、どのように表示されますか?
私が試したワット:
1.アプリ サンドボックスに次のようなフォルダーを作成しました。
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.次に、App SandBox 内の html、js ファイルを App Bundle から次のようにコピーしました。
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];
必要なファイルはすべて「folder」という名前のサンドボックス フォルダーにあるので、保存した html ファイルを次のように UIWebView で開いてみました。
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:pathToHtmlFileinDocumentsDic relativeToURL:[NSURL URLWithString:aFolderPath]]]];
これは動作しません。