我有一个保存在文档目录中的PDF文件。该文件的路径存储在NSString属性" self.URL'中。 以下是我展示活动项目的代码:
-(void)presentDocumentInteractionController{
self.docController = [UIDocumentInteractionController interactionControllerWithURL:self.URL];
self.docController.delegate = self;
[_docController setUTI:@"com.adobe.pdf"];
[_docController presentOptionsMenuFromBarButtonItem:self.activityBarButton animated:YES];
}
在iOS 8之前,这段代码运行良好。向用户呈现了诸如打印,复印和电子邮件之类的项目列表以供选择。升级到iOS 8 / XCode 6之后,我收到了这个运行时错误(它没有崩溃应用程序):
Unknown activity items supplied: (
"<UITextViewPrintFormatter: 0x7f908ba53ca0>"
)
我该如何解决这个问题?
答案 0 :(得分:5)
我遇到了同样的问题,并已切换到使用UIActivityViewController
。然而,这使得能够打开PDF的应用程序不再显示,所以也许这不是你想要的。
如果您想做最少的工作,您甚至不需要将PDF读入NSData
,使用NSURL
作为活动项目,iOS似乎知道该怎么做:
- (void)share:(id)sender
{
UIActivityViewController *activity =
[[UIActivityViewController alloc] initWithActivityItems:@[self.URL]
applicationActivities:nil];
if ([activity respondsToSelector:@selector(popoverPresentationController)]) {
activity.popoverPresentationController.barButtonItem = <# BAR BUTTON ITEM #>;
}
[self presentViewController:activity animated:YES completion:NULL];
}
让您的视图控制器遵守UIActivityItemSource
协议,然后您可以执行以下操作:
- (void)share:(id)sender
{
self.pdfData = [NSData dataWithContentsOfURL:self.URL];
UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:@[self] applicationActivities:nil];
if ([activity respondsToSelector:@selector(popoverPresentationController)]) {
activity.popoverPresentationController.barButtonItem = <# BAR BUTTON ITEM #>;
}
[self presentViewController:activity animated:YES completion:NULL];
}
如果您有PDF文件,则遵守协议相对简单。您当然可以优化并返回较小的NSData甚至是预览图像,但最低限度地执行此操作:
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
return _pdfData;
}
- (id)activityViewController:(UIActivityViewController *)activityViewController
itemForActivityType:(NSString *)activityType
{
return _pdfData;
}
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController
subjectForActivityType:(NSString *)activityType
{
return self.title;
}
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController
dataTypeIdentifierForActivityType:(NSString *)activityType
{
return @"com.adobe.pdf";
}
答案 1 :(得分:0)
这解决了我的问题:
ObjC:
dispatch_async(dispatch_get_main_queue(), ^() {
[_docController presentOptionsMenuFromRect:button.bounds inView:button animated:YES];
});
夫特:
if let docController = UIDocumentInteractionController(URL: url) {
dispatch_async(dispatch_get_main_queue()) {
docController.presentPreviewAnimated(true)
}
} else {
// handle nil docController
}
我不使用swift,因此此代码可能无效。
答案 2 :(得分:0)
使用
- (BOOL)presentOpenInMenuFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;
而不是
- (BOOL)presentOptionsMenuFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;