Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I associate file types with an iPhone application?

On the subject of associating your iPhone app with file types.

In this informative question I learned that apps could be associated with custom URL protocols.

That was almost one year ago and since then Apple introduced 'Document Support' which goes a step further and allows apps to associate with file types. There is a lot of talk in the documentation about how to set up your app to launch other appropriate apps when it encounters an unknown file type. This means the association doesn't work out of the box for any app, like the URL protocol registering did.

This leads me to the question: have system apps like Safari or Mail implemented this system for choosing associated applications, or will they do nothing, as before?

like image 401
Mihai Damian Avatar asked May 05 '10 14:05

Mihai Damian


People also ask

How do I associate file types with applications?

Open the Control Panel. In the Control Panel, click the Default Programs option. Click the Associate a file type or protocol with a program option. In the Default apps window, scroll to the bottom and select Choose defaults by file type.

How do I open a file with a specific app iOS?

Open a file once with a specific appSelect the file in the Finder, choose File > Open With, then choose an app. Control-click the file, choose Open With, then choose an app. Open the app, then choose File > Open.

What file type does iPhone use for apps?

ipa (iOS App Store Package) file is an iOS application archive file which stores an iOS app. Each . ipa file includes a binary and can only be installed on an iOS or ARM-based MacOS device.


2 Answers

File type handling is new with iPhone OS 3.2, and is different than the already-existing custom URL schemes. You can register your application to handle particular document types, and any application that uses a document controller can hand off processing of these documents to your own application.

For example, my application Molecules (for which the source code is available) handles the .pdb and .pdb.gz file types, if received via email or in another supported application.

To register support, you will need to have something like the following in your Info.plist:

<key>CFBundleDocumentTypes</key> <array>     <dict>         <key>CFBundleTypeIconFiles</key>         <array>             <string>Document-molecules-320.png</string>             <string>Document-molecules-64.png</string>         </array>         <key>CFBundleTypeName</key>         <string>Molecules Structure File</string>         <key>CFBundleTypeRole</key>         <string>Viewer</string>         <key>LSHandlerRank</key>         <string>Owner</string>         <key>LSItemContentTypes</key>         <array>             <string>com.sunsetlakesoftware.molecules.pdb</string>             <string>org.gnu.gnu-zip-archive</string>         </array>     </dict> </array> 

Two images are provided that will be used as icons for the supported types in Mail and other applications capable of showing documents. The LSItemContentTypes key lets you provide an array of Uniform Type Identifiers (UTIs) that your application can open. For a list of system-defined UTIs, see Apple's Uniform Type Identifiers Reference. Even more detail on UTIs can be found in Apple's Uniform Type Identifiers Overview. Those guides reside in the Mac developer center, because this capability has been ported across from the Mac.

One of the UTIs used in the above example was system-defined, but the other was an application-specific UTI. The application-specific UTI will need to be exported so that other applications on the system can be made aware of it. To do this, you would add a section to your Info.plist like the following:

<key>UTExportedTypeDeclarations</key> <array>     <dict>         <key>UTTypeConformsTo</key>         <array>             <string>public.plain-text</string>             <string>public.text</string>         </array>         <key>UTTypeDescription</key>         <string>Molecules Structure File</string>         <key>UTTypeIdentifier</key>         <string>com.sunsetlakesoftware.molecules.pdb</string>         <key>UTTypeTagSpecification</key>         <dict>             <key>public.filename-extension</key>             <string>pdb</string>             <key>public.mime-type</key>             <string>chemical/x-pdb</string>         </dict>     </dict> </array> 

This particular example exports the com.sunsetlakesoftware.molecules.pdb UTI with the .pdb file extension, corresponding to the MIME type chemical/x-pdb.

With this in place, your application will be able to handle documents attached to emails or from other applications on the system. In Mail, you can tap-and-hold to bring up a list of applications that can open a particular attachment.

When the attachment is opened, your application will be started and you will need to handle the processing of this file in your -application:didFinishLaunchingWithOptions: application delegate method. It appears that files loaded in this manner from Mail are copied into your application's Documents directory under a subdirectory corresponding to what email box they arrived in. You can get the URL for this file within the application delegate method using code like the following:

NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey]; 

Note that this is the same approach we used for handling custom URL schemes. You can separate the file URLs from others by using code like the following:

if ([url isFileURL]) {     // Handle file being passed in } else {     // Handle custom URL scheme } 
like image 66
Brad Larson Avatar answered Oct 06 '22 04:10

Brad Larson


In addition to Brad's excellent answer, I have found out that (on iOS 4.2.1 at least) when opening custom files from the Mail app, your app is not fired or notified if the attachment has been opened before. The "open with…" popup appears, but just does nothing.

This seems to be fixed by (re)moving the file from the Inbox directory. A safe approach seems to be to both (re)move the file as it is opened (in -(BOOL)application:openURL:sourceApplication:annotation:) as well as going through the Documents/Inbox directory, removing all items, e.g. in applicationDidBecomeActive:. That last catch-all may be needed to get the app in a clean state again, in case a previous import causes a crash or is interrupted.

like image 32
mvds Avatar answered Oct 06 '22 05:10

mvds