2011-10-03 123 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

相关问题