How to tell (programmatically) if there are / are

2019-03-28 19:52发布

问题:

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?

回答1:

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];
}


回答2:

//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];
    }


}


标签: iphone ios uti