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:

- (UIDocumentInteractionController *)controller {
    
    if (!_controller) {
        _controller = [[UIDocumentInteractionController alloc]init];
        _controller.delegate = self;
    }
    return _controller;
}

- (IBAction)buttonPressed:(id)sender {
    
    // here's a URL from our bundle
    NSURL *documentURL = [[NSBundle mainBundle]URLForResource:@"Colours" withExtension:@"zip"];
    
    // pass it to our document interaction controller
    self.controller.URL = documentURL;
    
    // present the preview
    [self.controller presentPreviewAnimated:YES];
}

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:

- (IBAction)sendButtonPressed:(id)sender {
    
    // send a ZIP file over to Dropbox
    NSURL *zipURL = [[NSBundle mainBundle]URLForResource:@"Colours" withExtension:@"zip"];
    self.controller.URL = zipURL;
    
    if (![self.controller presentOpenInMenuFromBarButtonItem:self.openButton animated:YES]) {
        
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"You don't have an app installed that can handle ZIP files." delegate:self cancelButtonTitle:@"Thanks!" otherButtonTitles:nil, nil];
        [alertView show];
    }
}

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:

#pragma mark - Delegate Methods

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
    
    return  self;
}

- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
    
    NSLog(@"Starting to send this puppy to %@", application);
}

- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {
    
    NSLog(@"We're done sending the document.");
}

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