UIDocumentInteractionController stopped working iOS 8

南笙酒味 提交于 2019-12-09 13:03:21

问题


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];
}

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?


回答1:


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.

Minimal Solution:

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];
}

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];
}

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



回答2:


Use

- (BOOL)presentOpenInMenuFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;

Instead of

- (BOOL)presentOptionsMenuFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;



回答3:


This solved the problem for me:

ObjC:

dispatch_async(dispatch_get_main_queue(), ^() {
    [_docController presentOptionsMenuFromRect:button.bounds inView:button animated:YES];
});

Swift:

if let docController = UIDocumentInteractionController(URL: url) {
    dispatch_async(dispatch_get_main_queue()) {
         docController.presentPreviewAnimated(true)
    }
} else {
    // handle nil docController 
}

I don't use swift so this code might not work.



来源:https://stackoverflow.com/questions/26005087/uidocumentinteractioncontroller-stopped-working-ios-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!