我们有一个代码,可以使用SOAP协议从服务器下载PDF。
我们将文件存储在iOs设备的Documents文件夹中,我们希望允许用户打开文件(通常是PDF文件)
但是,至少在模拟器上,应用程序没有成功打开文件。
当然,我可以手动打开文件(为了检查文件是否已经完美下载)
// 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];
}
模拟器中的文件完整路径是:
/ Users / User / Library / Application Support / iPhone Simulator / 4.3.2 / Applications / 87800296-2917-4F26-B864-B20069336D1D / Documents / 0000907.pdf
有人知道是不是有问题吗?
该文件可以是和电子表格,PDF格式,图像,文档......但是对于第一次工作来说,这对于pdf来说非常棒。
谢谢,
答案 0 :(得分:3)
我的控制器实现了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];
使用下一个方法,您可以在apple developer库中找到它
- (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;
}
谢谢,
答案 1 :(得分:1)
您无法使用UIApplication
的{{1}}方法打开文件网址。由于沙盒,其他应用程序无法访问应用程序的Documents文件夹中的任何文件。
您需要使用openURL:
打开您在其他应用程序中下载或创建的文档。