0

我很绝望UIDocumentInteractionController。我唯一想做的就是将我的应用程序中的 pdf 文件与所有其他注册 PDF 的应用程序共享。

这是我的代码:

NSString * filePath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
self.docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
self.docController.delegate = self;

NSURL *url = [NSURL fileURLWithPath:filePath];
BOOL isValid = [[UIApplication sharedApplication] canOpenURL:url];
NSLog(@"uti: %@", [self.docController UTI]);

//[self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
[self.docController presentOpenInMenuFromBarButtonItem:self.backBut animated:YES];

isValid变量始终为 TRUE,UTI 记录为 com.adobe.pdf(这似乎是正确的),但调用presentOpenFrom...两个调用(一个已注释)始终返回 NO。

我正在用模拟器测试,是这个问题吗?是否有人看到代码有问题或知道要检查什么?

4

3 回答 3

1

如果您正在处理 PDF 文件,那么您应该有任何能够在您的设备(模拟器)上打开 PDF 文件的应用程序。您可能想要安装PDF File Viewer,因为它是开源的。通过从 Xcode 运行安装应用程序后,您UIDocumentInteractionController将正确显示。

这是一个示例代码,用于从 Web 下载任何文档UIDocumentInteractionController并将其传递给另一个应用程序。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir   == NO)
{
    [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}
NSString *filePath = [cachePath stringByAppendingPathComponent:url.lastPathComponent];
NSData *fileData = [NSData dataWithContentsOfURL:url];
[fileData writeToFile:filePath atomically:YES];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:filePathURL];
[self.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
于 2015-03-11T12:20:10.353 回答
0

如果此实施所需的只是 PDF 的预览,Kenney 的解决方案仍然适用。但是,它并没有解决presentOpenInMenuFromRect或presentOpenInMenuFromBarButtonItem的原始问题。这只会在预览中显示 pdf,而不是实际的“打开方式...”对话框。除了使用设备来启动 Open In 控制器之外,没有已知的解决方案。

presentOpenInMenu... 方法检查系统是否下载了任何已注册的 PDF 阅读器。由于iOS模拟器没有提供,也没有办法添加,所以这个功能只能在设备上测试。

于 2013-08-14T19:58:08.800 回答
0

尝试这个:

NSString* path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"];
if (path) {
    NSURL* url = [NSURL fileURLWithPath:path];
    UIDocumentInteractionController* docController = [UIDocumentInteractionController interactionControllerWithURL:url];
    docController.delegate = self;
    [docController presentPreviewAnimated:YES];
}
于 2013-01-09T13:17:02.827 回答