How to check if any applications are associated with a file extension

I want to make the function "Open in .." in my iOS application.

Is there a way to check if any application on this device is associated with the file extension I want to provide?

If there are no applications on the current device to open a file with a file extension that the UIDocumentInteractionController will not be displayed after clicking the "Open in .." button, but I do not want to show this button in this case.

So, the question is: how to check if any application on the device can open any file with a specific extension?


Update:

For example, the UIDocumentInteractionController has the NSArray icons property.

It contains images of all applications that can open a file with my extension. But if there are no applications, it displays an image of an empty application.

Therefore, I can not check it, for example, using docInteractionController.icons.count == 0 . I am looking for other tricks.

Thanks in advance.

+7
source share
3 answers

Although the UIDocumentInteractionController does not offer a way to know in advance if there are any applications that can process the document, -presentOpenInMenuFromRect: will return a flag indicating if there are any applications that can open the document. This requires that you already configure and submit the controller, which is not optimal.

There is a workaround for this, a little hacked, but functional: before calling the "real" interaction controller, create a dummy using a dummy document, and present it from the box border rectangle. This ensures that it "appears" off the screen, so your user will not see it. At this point, you have the flag returned with -present , and you can immediately remove the dummy controller and continue displaying it.

+5
source

On OSX, you can get a list of application package identifiers that can handle a specific type of content using LSCopyAllRoleHandlersForContentType . But on iOS, I don’t think there is such a way.

If I find, I will edit my answer.

Given that you are looking for other tricks, you can check if this single image in the icon array is a standard document icon.

If so, you know that there is no application associated with this file type. But this approach will be OS dependent as the icon of the shared file may change.

+1
source

From the official documentation:

To declare its support for file types, your application should include CFBundleDocumentTypes in the Info.plistproperty list file. (See “Core Foundation Keys.”) The system adds this information to the registry, which other applications can access through the interaction with the controller documents.

For me, this means that access to the registry is possible only through the UIDocumentInteractionController, and therefore no, you won’t be able to know in advance if there are any available applications for the file format (which is fully consistent with Apple’s philosophy not allowing applications to interact directly with each other).

UPDATE: as you said, the icon property contains an image even without any applications present. I checked, and all the other methods and properties of the controller do not give hints about applications that can open the current file format. You said that no application can open the specified file format, there is an "image of an empty application." Maybe you can extract this icon, and when the array icons will have only one image, check that the selected image and the icon match?

0
source

All Articles