1

I would like to know how to present the "Open In..." Action Sheet (iPhone) / Popover (iPad) from my app, preferably an IBAction

I would hope that it'd be similar to declaring a file type then creating the view and opening the app selected by the user, but I know it is more complicated then that.

I realize that a similar question has been asked on StackOverflow, but I cannot make sense of the answer that was accepted: How to use "open in..." feature to iOS app?, and I have found some Apple Documentation on Document Interaction Programming. But, I can't really make sense of these.

Community
  • 1
  • 1
Sam Spencer
  • 8,492
  • 12
  • 76
  • 133

3 Answers3

13

Create a UIDocumentInteractionController by using the interactionControllerWithURL: class method (pass the URL of the file you want to open in another app).

Then call either presentOpenInMenuFromRect:inView:animated: or presentOpenInMenuFromBarButtonItem:animated:. The controller takes care of presenting the popover with available apps for that file type and opening the selected app.

If you want to know when the menu was dismissed and which app was selected, you need to implement the UIDocumentInteractionControllerDelegate protocol.

omz
  • 53,243
  • 5
  • 129
  • 141
5

omz makes some good points on how to do that in his answer, however this procedure is much easier with the introduction of new APIs in iOS 6. Here's a simple and efficient way to show the UIActionSheet Open-In-Menu in iOS 6 and up:

NSArray *dataToShare = @[contentData];  //Or whatever data you want to share - does not need to be an NSArray
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];

Also, if your app is compatible with versions of iOS lower than 6.0 you may want to check if the Share Service exists:

if ([UIActivityViewController class])

Once you present the sheet, iOS will automatically handle the rest for you. It will display a beautiful with icons showing each app or service the user can open / share your data with:

iOS 6 Share Sheet

Note that depending on the contents of the data, iOS will show different services in the Share Sheet

EDIT: The method above shares the file content, but not the file itself. Refer to omz's answer for more on that.

Community
  • 1
  • 1
Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
2

I've personally never had to do this, but your answer can most certainly be found in this Apple Documentation: http://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/DocumentInteraction_TopicsForIOS/Articles/PreviewingandOpeningItems.html#//apple_ref/doc/uid/TP40010410-SW1.

elliottbolzan
  • 1,057
  • 1
  • 15
  • 30