2011-11-10 12 views
1

ローカルに保存したドキュメント(Wordドキュメント、画像、またはビデオ)を表示しようとしています。私はUIWebViewを使用していますが、Simulatorでは完全に動作しますが、デバイス上では空白の画面です(コンソールにスローされたエラーはありません)。ドキュメントを表示するコードは次のとおりです。UIWebViewは、ローカルに格納されたドキュメントをシミュレータに表示しますが、デバイスは表示しません。

UIWebView *newWebView; 
if (appDelegate.IS_IPAD) 
{ 
    newWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)]; 
} 
else 
{ 
    newWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; 
} 
newWebView.scalesPageToFit = YES;  
[self setWebView: newWebView]; 
[newWebView release], newWebView = nil; 

[[self webView] setDelegate: self]; 
[[self view] addSubview:[self webView]]; 
NSURL *nsURL = [NSURL fileURLWithPath:filePath isDirectory:NO]; 
[[self webView] loadRequest:[NSURLRequest requestWithURL:nsURL]]; 

表示する前にドキュメントをローカルに保存しています。私は、ファイルがシミュレータ上のようにデバイスに正常に保存されていると思います。

// Save the file on the device 
    NSURL *contentUrl = [NSURL URLWithString:selectedContent.contentUrl]; 
    NSString *fileName = [[contentUrl absoluteString] lastPathComponent];  
    NSString *homeDirectoryPath = NSHomeDirectory(); // Create the path 
    NSString *unexpandedPath = [homeDirectoryPath stringByAppendingString:@"/MyApp/"]; 
    NSString *folderPath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedPath stringByExpandingTildeInPath]], nil]]; 
    NSString *unexpandedImagePath = [folderPath stringByAppendingFormat:@"/%@", fileName]; 
    NSString *filePath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedImagePath stringByExpandingTildeInPath]], nil]]; 
    if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath isDirectory:NULL]) 
    { 
     [[NSFileManager defaultManager] createDirectoryAtPath:nil withIntermediateDirectories:NO attributes:nil error:nil];   
    } 
    // Do the actual write of the file to the device 
    NSData *fileData = [NSData dataWithContentsOfURL:myUrl]; // Create the file data reference 
    [fileData writeToFile:filePath atomically:YES]; //Save the document to the home directory 

答えて

2

ケースの問題に注意してください。シミュレータは大文字と小文字は区別されませんが、iPhoneは!

+0

おかげで、私は私が--- [NSURL fileURLWithPath「filePathに」の正確な同じ値を使用しているとして、それはケースの問題をすることはできませんかなり確信しています: filePath isDirectory:NO]; ---コンテンツをローカルに保存するときと同じように。私はNSLogのfilePathの値を表示していますし、大文字小文字の区別がついています。大文字と小文字の区別がついていますか? – Phamer

+0

それについての私の質問を見てくださいhttp://stackoverflow.com/questions/6957216/load-local-image-into-uiwebview – Piotr

+0

+1問題の場合、それはちょうど "資産/メディアの問題だった.. 。 "代わりに"資産/メディア... "、すべての自動生成されたjavascriptで... – dulgan

0

私はドキュメントをデフォルトのドキュメントディレクトリに保存してこれを解決しました。ここで私はこれを達成するために使用されるコードは次のとおりです。

NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docDirectory = [arrayPaths objectAtIndex:0]; 
    NSString *filePath = [docDirectory stringByAppendingString:@"/"]; 
    filePath = [filePath stringByAppendingString:fileName]; 
    NSData *fileData = [NSData dataWithContentsOfURL:myUrl]; 
    [fileData writeToFile:filePath atomically:YES]; 
関連する問題