How to associate your Swift app to open a PDF from Gmail?

So you’re working on an app where a user can sign a document with Apple Pencil, but not sure how to import a document into your app? Example, your client send a very important document to sign on your Gmail (or Mail) in iPhone. Now to open the document with your application to sign? This post let you understands how to associate your app to open a PDF document from various apps. You can not just import a PDF but any supported files from various applications.

Add the following in your Info.plist file.

<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>PDF Document</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
</dict>
</array>

To edit, Info.plist > [Right Click] Open As > Source Code. Copy and Paste above values in before:

Screen Shot 2017-05-21 at 4.41.55 PM.png

Figure 1. Showing how to edit Info.plist file.

</dict>
</plist>

This should look like this:

Screen Shot 2017-05-21 at 5.03.19 PM

Figure 2. Showing how an Info.plist should look alike.

By adding this key, let iOS will show your app when a user tries to open a PDF in Gmail or any other application which uses UIDocumentInteractionController to share the content with other apps.

So what about each value?

  1. Document type (CFBundleDocumentTypes): It is an Array type to add the type of documents which your application supports, should have separate entries of Dictionaries.
  2. Document Type Name (CFBundleTypeName): It is a String type value to specify the document type. Example: PDF Document.
  3. Role (CFBundleTypeRole): Your applications role can be an Editor, Viewer, Shell, Quick Look Generator or None. There are multiple options for this value, you can set the one as per your use case. Example: Viewer
  4. Handler rank (LSHandlerRank): It is a String type value to show who you are for this document? Are you the owner of this document? or editor? Example: Alternate, a secondary viewer of this file type. There are multiple options for this value, you can set the one as per your use case.
  5. Document Content Type UTIs (LSItemContentTypes): It is an Array type value to specify the Uniform Type Identifiers to handle. This will filter and show your app if it matches a document type having the same UTI as you have specified in Info.plist. Example: com.adobe.pdf

To handle the call back from Gmail to your app, you should do the following:

In AppDelegate.swift,

//Before iOS 10

func application(application: UIApplication, openURL url: URL, sourceApplication: String, annotation: Any?) -> Bool {
    //You should handle the call back here.
    return true
}
//iOS 10 onwards

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    //You should handle the call back here.
    return true
}

When you try to open a PDF document in Gmail, you should see like this:

Figure 3. Showing our PDF handler inside the Gmail app.

See the iSign app button. By tapping on it, it will take user to iSign app where we are handling the call back. We are getting the PDF document and do the rest of the things inside the app.

Disclaimer: This is not the original iSign app. It is just a prototype for the reference.

What happens when you click on an app button inside the UIDocumentInteractionController?

  • iOS will make a copy of the same document (in our case a PDF) into the handling application document directory. You should get the URL of copied file in the above delegate.
  • You will have the local URL of the file and from where you get this file (source app bundle identifier).

References:

I found this old but yet nice as an alternate reference for this post.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s