Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell (programmatically) if there are / are not any registered apps that support opening a specific document type?

Tags:

ios

iphone

uti

Apple's documentation for UIDocumentInteractionController presentOpenInMenuFromBarButtonItem:animated: method states that "If there are no registered apps that support opening the document, the document interaction controller does not display a menu." In my app I want to display a button if and only if there is an app on the device that will open it. (I only want the button to pop up a menu to open a file; I don't want QuickLook, Copy or Print). As things stand, if the button is there, but no apps are registered that can open the relevant file, the user gets the unsatisfactory experience of a button that does nothing when tapped.

So - can I find out whether or not there are any / no registered apps that support opening a specific document type? Clearly, UIDocumentInteractionController instances can find this out. Is there a public API way of finding it out?

like image 787
Obliquely Avatar asked Jun 04 '11 00:06

Obliquely


2 Answers

OK, more research reveals a stackoverflow user frenchkiss-dev has a solution - derived from reading the docs more carefully than me and some lateral thinking. My code below, based on frenchkiss-dev's answer, sits in a ViewDidAppear method and disables my button if opening and then closing the open file menu (without animation) reveals that there are no apps that can handle opening the file. The context for this snippet is that a UIDocumentInteractionController has already been set up in viewDidLoad and is accessed via [self docInteractionController].

BOOL isAnAppToOpenURL = [[self docInteractionController] presentOpenInMenuFromRect:CGRectZero inView:[self view] animated: NO];
[[self docInteractionController] dismissMenuAnimated:NO];

if (!isAnAppToOpenURL)
{
    // iOS think NO app is present on the device that
    // can open the URL set on the UIDocumentInteractionController
    [[self openFileButton] setEnabled:NO];
}
like image 88
Obliquely Avatar answered Nov 14 '22 01:11

Obliquely


//Connect up theOpenInBtn in IB


@interface DocumentViewerViewController ()
{

    IBOutlet UIWebView *webView;
    NSURL *fileURL;
    NSData *fileOnline;
    UIDocumentInteractionController *dic;
    IBOutlet UIBarButtonItem *theOpenInBtn;

}


(void)viewDidLoad
{
     [super viewDidLoad];


    BOOL isAnAppToOpenURL = [dic presentOpenInMenuFromRect:CGRectZero inView:[self view] animated: NO];
    [dic dismissMenuAnimated:NO];

    if (!isAnAppToOpenURL)
    {
        // iOS think NO app is present on the device that
        // can open the URL set on the UIDocumentInteractionController
        [theOpenInBtn setEnabled:NO];
    }


}
like image 25
Dave Kozikowski Avatar answered Nov 14 '22 03:11

Dave Kozikowski