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

余生颓废 提交于 2019-12-12 08:04: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];
    }


}


来源:https://stackoverflow.com/questions/6233837/how-to-tell-programmatically-if-there-are-are-not-any-registered-apps-that-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!