2011-10-03 120 views
1

我們有一個代碼可以使用SOAP協議從服務器下載PDF。從iOS應用程序打開文件查看器(PDF)

我們存儲在iOS設備的文檔文件夾中的文件,以及我們希望允許用戶打開該文件(通常是PDF格式的文件)

但是,至少在模擬器上,應用程序沒」 t成功打開文件。

當然,我可以手動打開該文件(爲了檢查該文件已被完全下載)

// Get the documents folder where write the content 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:documentDownloaded.fileName]; 

if ([[NSFileManager defaultManager] fileExistsAtPath:[self getFileCompletePath]]){ 
    NSString *filePath = [[NSString alloc] initWithFormat:@"%@", [self getFileCompletePath]]; 
    NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath]; 

    if ([[UIApplication sharedApplication] canOpenURL:url]){ 
     [[UIApplication sharedApplication] openURL: url]; 
    } else { 
     NSLog(@"Not supported application to open the file %@", filePath); 
    } 

    [url release]; 
    [filePath release]; 
} 

在模擬器的文件的完整路徑是:

/用戶/用戶/ Library/Application Support/iPhone Simulator/4.3.2/Applications/87800296-2917-4F26-B864-B20069336D1D/Documents/0000907.pdf

任何人都知道是否有問題?

該文件可能是和電子表格,pdf,圖像,文檔......但對於第一個解決方法將是偉大的只是PDF的。

謝謝

回答

1

你不能用UIApplicationopenURL:方法打開的文件的URL。由於沙盒,其他應用程序無法訪問應用程序的「文檔」文件夾中的任何文件。

您需要使用UIDocumentInteractionController才能打開您下載或在其他應用程序中創建的文檔。

+0

昨天我在讀關於蘋果庫這個組件,我今天檢查,但是...如果我沒有錯,當我使用這個代碼行... //獲取寫入內容的文檔文件夾 NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString * documentsDirectory = [paths objectAtIndex:0]; 我正在訪問用戶文檔文件夾,而不是應用程序文檔文件夾。 –

3

omz你是對的。最後的解決方案是下一個。

我的控制器中實現的UIDocumentInteractionControllerDelegate

當用戶想打開的文檔的代碼是下一個:

NSString *filePath = [[NSString alloc] initWithFormat:@"%@", [self getFileCompletePath]]; 
    NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath]; 
    UIDocumentInteractionController *documentController = [self setupControllerWithURL:url 
                     usingDelegate:self]; 

    [documentController retain]; 
    [documentController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES]; 
    [url release]; 
    [filePath release]; 

使用下一個方法,你可以發現它在蘋果開發者庫

- (UIDocumentInteractionController *) 
    setupControllerWithURL: (NSURL *) fileURL 
    usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate { 

    UIDocumentInteractionController *interactionController = 
     [UIDocumentInteractionController interactionControllerWithURL: fileURL]; 
    interactionController.delegate = interactionDelegate; 

    return interactionController; 
} 

最後我們包含代理方法:

#pragma mark methods for the UIDocumentInteractionControllerDelegate 

- (void)previewDocumentWithURL:(NSURL*)url 
{ 
    UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url]; 
    preview.delegate = self; 
    [preview presentPreviewAnimated:YES]; 
    [preview retain]; 
} 

- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller 
{ 
    [controller autorelease]; 
} 

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{ 
    return self; 
} 

- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{ 
    return self.view.frame; 
} 

- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{ 
    return self.view; 
} 

謝謝

+0

如果說你的內容是一個字節流而不是文件,是否有定義mimetype?如果你已經在內存中使用正確的擴展名,將文件寫入磁盤似乎很愚蠢。 UIWebView在使用LoadData時支持這一點。 – slott

相關問題