How to register file types supported by an iOS app

If you would like other applications to open your app based on a file type, you can register your document request in the application plist file. This provides a means to associate any file type with your iOS app.

Include the CFBundleDocumentTypes key, which is an array of dictionaries, where each entry defines information about the file type(s) your app supports.

Let’s look at a typical definition:

<key>CFBundleDocumentTypes</key>
<array>
  <dict>
    <key>CFBundleTypeName</key>
    <string>XML File</string>
    <key>LSHandlerRank</key>
    <string>Default</string>
    <key>CFBundleTypeRole</key>
    <string>Viewer</string>
    <key>LSItemContentTypes</key>
    <array>
      <string>public.XML</string>
    </array>
    <key>CFBundleTypeIconFiles</key>
    <array>
      <string>Sandbox-icon.png</string>
    </array>
  </dict>
</array>

CFBundleTypeName is the name of the document type.

LSHandlerRank specifies if the file type is owned by the app or if the app can only open files of the specified type. Default is a common value for this key.

CFBundleTypeRole specifies the role of the application as it relates to documents that match the file type. Values include Editor, Viewer and None, each with varying levels of permissions, from read/save to no access.

LSItemContentTypes is an array of Uniform Type Identifiers (UTI) of the file type(s) supported. In the example above the value is set to the UTI for XML files (public.XML).

CFBundleTypeIconFiles is an array of values that specify the icons to associate with your app and file type. For example, you could include two different icons, one low and one high resolution.

Calling an Application Based on File Extension or File Type

The easiest way to see how this works is install the application with the file extensions configured and to email yourself a file. For example, in the screenshot below I emailed a file (fubar.xml) and did a tap and hold on the file. The options available are shown below, the Open in Sandbox selection is a reference to my test application:

If I select the option Open In… from the list above, the display now shows the icon configured above along with a reference to all applications that are configured to support XML files:

How an App is Invoked

If an application is called through a file type association, the method didFinishLaunchingWithOptions will be called. Within this method you can check for the key UIApplicationLaunchOptionsURLKey which will contain a NSURL object that the original application called via an openURL call, which in turn launched the app associated with the file type.

In the code snippet below, the url is used to read the contents of the file and the information written to a textview:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // Create and initialize window
  window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
  // Create view controller
  vc = [[SandboxViewController alloc] init];
 
  NSURL *url = [launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];  
  if (url != nil)
  {
    NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"The file contained: %@",str);
 
    // Set the text read from the file
    [[vc textView] setText:str];
  } 
 
  [window addSubview:vc.view];
  [window makeKeyAndVisible];
 
  return YES;
}

The output of the application looks as follows:

Cleanup Documents Directory

When a file is passed to an application based on a registered file type, the file will be copied into the Documents directory in a folder titled Inbox. For example, the url path will look similar to this:

file://localhost/private/var/mobile/Applications/517352F5-E373-4869-8104-50E21D8539DA/Documents/Inbox/fubar.xml

You will need to delete the files in this folder as the system will not do this for you.

Download Xcode Project

I’ve included a very basic application that you can use to see how all this comes together. Install the application on a device and email yourself a file with the extension .xml

XCode Project – File Type Associations