UIDocumentInteractionController停止工作iOS 8 [英] UIDocumentInteractionController stopped working iOS 8

查看:132
本文介绍了UIDocumentInteractionController停止工作iOS 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文档目录中保存了一个PDF文件。文件的路径存储在NSString属性self.URL中。
以下是我展示活动项目的代码:

I have a PDF file saved in the document directory. The path to the file is stored in a NSString property 'self.URL'. Here is my code to present the activity items:

-(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后,我收到此运行时错误(它不会使应用程序崩溃):

Before iOS 8 this code worked fine. The user was presented with a list of items such as print, copy and email to choose from. After upgrading to iOS 8 /XCode 6, I'm getting this runtime error (it doesnt crash the app):

Unknown activity items supplied: (
    "<UITextViewPrintFormatter: 0x7f908ba53ca0>"
)

我该如何解决这个问题呢?

How can I solve this problem?

推荐答案

我遇到同样的问题,并已切换到使用 UIActivityViewController 。然而,这使得能够打开PDF的应用程序不再显示,所以也许这不是你想要的。

I have the same problem and have switched to using UIActivityViewController. However this makes the apps capable of opening the PDF no longer show up, so maybe that's not what you want.

如果你想做最少的工作,你甚至不需要将PDF读入 NSData ,使用 NSURL 作为活动项目,iOS似乎知道该怎么做:

If you want to do minimal work, you don't even need to read your PDF into NSData, use a NSURL as activity item and iOS seems to know what to do:

- (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 协议,然后您可以执行以下操作:

Original Answer:

Make your view controller adhere to the UIActivityItemSource protocol, then you can do:

- (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甚至预览图像,但最低限度地执行此操作:

Adhering to the protocol if you have a PDF file is relatively simple. You can of course optimize and return smaller NSData and even a preview image, but minimally do this:

- (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";
}

这篇关于UIDocumentInteractionController停止工作iOS 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆