How to open items in other iOS Apps

There are several ways to send items from your own app to others, one of which is custom URL schemes. But not every app supports such schemes.

Another way is to use a UIDocumentInteractionController. This is an iOS View Controller you can give a URL with an item (or items) to open in other apps or preview right away (such as image, video, ZIP file, etc).

Screenshot 2014.01.09 13.26.10

iOS will handle the display of apps capable of handling your files. It’s fairly straightforward to setup such a complex sounding controller. It even has a convenience method, but note that you must retain this in your own property, otherwise your app will crash when the delegate returns with a call back.

Here’s how you create a UIDocumentInteractionController. I’m assuming you’ve got a property for it in your class called controller:

That’s a custom initialiser, followed by a method that would preview a document in your own view controller. The presentation will include its own “open in” button as well. However, not all items can be previewed, especially not on older versions of iOS.

You can also bring up an “Open In…” dialogue like this:

Here we pass a ZIP file to our controller and try to open it in another app (such as DropBox or Goodreader). If no such apps are installed an alert view will tell the user about this. You can do the same with the Preview dialogue from earlier.

Note that for the preview option you are required to implement the following UIDocumentInteractionControllerDelegate method:

The first one is required for the preview controller to function, the latter two are optional and keep track of where your data is while passing it to another app. It’s useful to have so we can display a progress indicator of sorts while a large file is being passed on.

Demo Project

I’ve created a demo project on GitHub which demonstrates this in more detail.

Enjoy!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.