Home > Articles > Mobile Application Development & Programming

  • Print
  • + Share This
This chapter is from the book

Recipe: Checking for the Open Menu

When using a document interaction controller, be aware of this rule of thumb: The options menu will almost always provide some kind of menu choices, especially if you implement the action and quick look callbacks. You may not, however, have any open-in options to work with.

That happens when there are no applications installed on a device that support the file type you are working with. This may be caused by an obscure file type, or more often, because the user has not yet purchased and installed any applications that offer that file type.

Because of this, you’ll want to check whether to offer an “Open” menu option in the first place. Recipe 16-4 performs a rather ugly test to see if external apps will offer themselves as presenters and editors for a given URL. What it does is this: It creates a new, temporary controller, and attempts to present it. If it succeeds, there are conforming file destinations installed on the device and the method immediately dismisses the controller. If not, there are no such apps, and any “Open In” buttons should be disabled.

This is obviously a rather dreadful implementation but it has the advantage of being able to test as you lay out your interface or when you start working with a new file. I encourage you to file an enhancement request at bugreporter.apple.com.

Recipe 16-4. Checking for External Document Support

// Determine whether a file URL can be opened in external apps

- (BOOL)canOpen: (NSURL *)fileURL

{

    UIDocumentInteractionController *tmp =

        [UIDocumentInteractionController

            interactionControllerWithURL:fileURL];

    tmp.delegate = self;

    BOOL success = [tmp presentOpenInMenuFromRect:CGRectZero

        inView:self.view animated:NO];

    [tmp dismissMenuAnimated:NO];

    return success;

}
  • + Share This
  • 🔖 Save To Your Account