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

送分小仙女□ 提交于 2019-12-03 14:20:23
Obliquely

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


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