2011-11-10 49 views
1

我遇到了一個問題,我試圖查看本地存儲的文檔(Word文檔,圖像或視頻)。我使用的是UIWebView,在模擬器上它可以很好地工作,但在設備上,它是一個空白屏幕(在控制檯中沒有錯誤)。這裏是我的代碼來查看文檔: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 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]; 
相關問題