How to check if any apps are associated with file extension
Asked Answered
K

3

7

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

Is there any way to check if any app on this device is associated with file extension that i want to share?

If there are no apps on current device to open file with such an extension than UIDocumentInteractionController will not be displayed after clicking on "Open in.." button, but i want not to show this button in such case.

So the question is: how to check if any app on device can open some file with specific extension?


Update:

For example UIDocumentInteractionController has NSArray property icons.

It contains images of all aplications that can open the file with my extension. But if there are no applications it contans image of empty application.

So i can't check it using docInteractionController.icons.count == 0 for example. I am looking for other tricks.'

Thanks, in advance.

Kleper answered 14/3, 2013 at 11:42 Comment(0)
T
5

Although UIDocumentInteractionController does not offer a way to discover in advance whether there are any applications that can handle a document, -presentOpenInMenuFromRect: will return a flag indicating whether there were any applications that could open a document. This requires you to have already set up and presented the controller, which is not optimal.

There is a workaround for this, a little hacky but is functional: Before you invoke the "real" interaction controller, create a dummy one using a dummy document, and present it from the rect of the window's bounds. This guarantees that it will "appear" offscreen, so your user won't see it. At that point, you have the flag returned from -present, and you can immediately dismiss the dummy controller, and the proceed to show your UI appropriately.

Tracytrade answered 14/3, 2013 at 14:51 Comment(1)
I think the solution with fake presentOpenInMenuFromRect matches here the best. Thank you.Kleper
B
1

On OSX, you can get a list of application bundle identifiers capable of handling a specific content type using LSCopyAllRoleHandlersForContentType. But on iOS, I don't think there is such a way.

If I find, I'll edit my answer.

Considering you are looking for other tricks, you can check if that one image in the icons array is the generic document icon.

If it is then you know that there is no app associated to handle that file type. But this approach will be OS version dependent as generic file icon may change.

Bauer answered 14/3, 2013 at 14:43 Comment(4)
I thought for such a stupid trick. To load docInteractionController with file that have fake extension, for example: @"*.dfsdifjsi";. Than i would get this image of empty application and save it. After that i will load it with my needed file and if icons.count == 1 i can compare received images. I must work,sure, but it is really stupid to do.Kleper
You don't have to get default icon image with fake extension. You have it now, just add it to your project (or declare it as a constant data) and use for comparison. If that icon image changes in the future versions of the iOS, you have to update your app too of course. Yes, this is not good, but your options are limited unless we find a better way.Bauer
Each device have other icon resolution, so better i think is to save it.Kleper
But better is to use the result of presentOpenInMenuFromRect method i think)Kleper
V
0

From the official documentation:

To declare its support for file types, your app must include the CFBundleDocumentTypes key in its Info.plistproperty list file. (See “Core Foundation Keys”.) The system adds this information to a registry that other apps can access through a document interaction controller.

To me this indicates that the registry can only be accessed through UIDocumentInteractionController and so no, you would not be able to know in advance if there are any available apps for the file format (which would be totally in line with Apple's philosophy of not letting apps interact directly with each other).

UPDATE: as you said the icons property contains an image even with no applications present. I checked and all the other methods and properties of the controller do not give an hint about the apps that may open the current file format. You said in case that no app can open the specified file format there is an "image of empty application". Maybe you can extract that icon and when the array icons only has one image check if the extracted image and the icon are the same?

Viddah answered 14/3, 2013 at 14:7 Comment(1)
I think you didn't understand the question. I do not need to open any files from other applications.Kleper

© 2022 - 2024 — McMap. All rights reserved.