6

I have a problem in my app. After getting some statistics, I generate a PDF file, and I want to show an UIActivityViewController with the options "Open in iBooks" and "Send By Mail" mainly (others like "Open in Dropbox" would be great to).

Well the thing is that before trying to use UIActivityViewController, I was using UIDocumentInteractionController, with the following code:

self.docController = [UIDocumentInteractionController interactionControllerWithURL:url];
self.docController.delegate = self;
[_docController presentOpenInMenuFromRect:_openInIBooksButton.bounds inView:self.openInIBooksButton animated:YES];

Where url is a path like /Documents/../statistics.pdf. It worked, it showed a popover with the buttons open in iBooks and open in Dropbox, but not Send by Mail. Now I've changed it with the following code:

NSArray* itemsToShare = [NSArray arrayWithObjects:pdfData, nil];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];
activityVC.excludedActivityTypes = @[UIActivityTypeCopyToPasteboard]; //or whichever you don't need
[self presentViewController:activityVC animated:YES completion:nil];

Where pdfData is a NSData object. And it works too, but now it shows the option of sending it by email, but not the iBooks option. I'm going nuts because I don't find the reason of that behavior and I need the two options, iBooks and Mail.

I don't know if it has something to do with the fact that the UIDocumentInteractionController has a path which ends with .pdf and the UIActivityViewController only has a NSData object. But I can't find a solution for that.

Somebody has found that problem before?

Thank you very much.

diegomen
  • 1,643
  • 1
  • 21
  • 33
  • Did you try `presentOptionsMenuFromBarButtonItem` of UIDocumentInteractionController? - And I think that the `initWithActivityItems` argument of UIActivityViewController also accepts NSURL items in the array, that might help. – Martin R Apr 22 '13 at 19:04
  • Thanks for responding, but if you put an url in the array, it only shows the send by mail and publish on twitter options, and if you tap on send by mail, it opens the mail composer with a text containing the url, but without attaching the file.. – diegomen Apr 22 '13 at 19:41
  • OK, it was just an idea! What about presentOptionsMenuFromBarButtonItem? – Martin R Apr 22 '13 at 19:43

1 Answers1

10

When you use presentOpenInMenuFromRect:inView:animated: you only get a list of apps that can work with the given file.

What you want to use is presentOptionsMenuFromRect:inView:animated: which gives you the options that you are looking for.

rmaddy
  • 298,130
  • 40
  • 468
  • 517
  • Thank you very much, but if you open an attached PDF in a mail, you preview it and tap the button share, a popover is displayed and it has the options Open in iBooks and send by mail, how is it possible? – diegomen Apr 22 '13 at 19:37
  • Instead of using the `presentOpenInMenuFromRect:` method, use the presentOpenInMenuFromRect:` method of `UIDocumentInteractionController`. – rmaddy Apr 22 '13 at 20:14
  • Sorry, but you've said the same thing...? about what method are you talking about for replacing the presentOpenInMenuFromRect: ? – diegomen Apr 24 '13 at 16:56
  • Sorry about that (copy/paste error). Replace your call to `presentOpenInMenuFromRect:inView:animated:` with `presentOptionsMenuFromRect:inView:animated:`. – rmaddy Apr 24 '13 at 17:10
  • Yeah!!! that works like a charm! thank you very much! you're my hero! haha, how can I vote your last comment as the correct answer? – diegomen Apr 24 '13 at 18:45
  • I can't vote up because I have not enough reputation yet, but I marked as the correct answer, thank you very much again! – diegomen Apr 24 '13 at 18:57
  • @diegomen: Isn't that what I suggested in my first comment to your question ? – Martin R Apr 27 '13 at 05:03
  • @MartinR Nope, you suggested presentOptionsMenuFromBarButtonItem but the one I had to use is presentOptionsMenuFromRect:inView:animated. – diegomen Apr 30 '13 at 09:10
  • 1
    @diegomen so you actually didn't use UIActivityViewController? – Ji Fang Nov 13 '13 at 20:20
  • @JiFang UIActivityViewController only shows Activities but not "Open in..." options, even if there is only one activityItem. If your app allows multiple selection then it is a good idea to use the UIDocumentInteractionController for a single item, and UIActivityViewController for multiple items – Dale Mar 26 '16 at 22:29