UI Documentation Controller

In this blog we are going to see how to open/preview a document stored in iOS device.

In iOS we have a class called UIDocumentInteractionController which helps in achieving the above need of ours i.e., It helps us to preview document which are stored locally in the iOS device memory.

UIDocumentInteractionController provides an user interface for previewing a document or can open it in other apps that supports the file type and also it provides few other options like opening, copying, printing, sharing and so on for interacting with a specified file.

The file which we mention here may be of any kind such as pdf, image, videos, zip file, etc.

Using UIDocumentInteractionController we can mainly perform the following actions

        A. Preview a document
        B. Present a view which lists other applications which are all supports to open the specified file type and it also lists some of the actions like copying, printing, mailing, etc,… which acts upon the specified file. The following screenshots shows us the difference between the above two methods.
A & B

In this blogs let see how to preview a document, but before that lets see few basic details regarding UIDocumentInteractionController:

Availability : From iOS 3.2 and later
Subclass of : NSObject
Delegate : UIDocumentInteractionControllerDelegate

A.) 3 simple steps to use UIDocumentInteractionController to preview your file right away:

Step – 1. The first step is to create a instance of the class by passing the file URL as shown below,

// Initialising UIDocumentInteractionController with fileURL
let documentController: UIDocumentInteractionController = UIDocumentInteractionController(URL: fileURL)

where the fileURL is the path(URL) of the file which you would like to open.

let documentsURLPath = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL

let fileURLPath = documentsURLPath.URLByAppendingPathComponent(“YourFileName”)

– The above URL path denotes the path of the document named “YourFileName” present inside the document directory of your app memory.

Step – 2. Next step is to set the delegate property for the class and implement its required methods,

– Add the delegate “UIDocumentInteractionControllerDelegate” to the viewController where UIDocumentInteractionController is being used, then configure the delegate property as follows.

// Configure UIDocumentInteractionController
self.documentController.delegate = self

– After setting the delegate property we should set some of the following delegate methods to preview the document

               i.) In which one of the delegate method is “ documentInteractionControllerViewControllerForPreview” method, It tells the UIDocumentInteractionController class where to open the document, it returns a viewController, as of now we will use the same viewController as the resulting viewController by returning self.

func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
return self

               ii.) One another method is “presentPreviewAnimated” delegate method which specifis wether to allow or not to preview document. Returning “true” will enable preview mode.

func presentPreviewAnimated(animated: Bool) -> Bool {
return true

Step – 3. Now use the “presentPreviewAnimated” method of the class UIDocumentInteractionController to preview the file specified in the URL.

// Preview the specified file

When the above 3 line of code is executed a full-screen view will be presented by displaying the contents of the file that is presented in the given file URL.

B.) The second method can be implemented by the following methods,
•  presentOptionsMenuFromRect(_:inView:animated:) 
•  presentOptionsMenuFromBarButtonItem(_:animated:) 
•  presentOpenInMenuFromRect(_:inView:animated:) 
•  presentOpenInMenuFromBarButtonItem(_:animated:) 

We can use any of the above listed methods and present the user with various options like opening, copying, printing, mailing, etc.,

You can notice that there is a difference in the above mentioned methods namely,

– OptionsMenu and
– OpenInMenu

OptionsMenu : This will list all the apps which are capable of opening the specified file and also this will list possible options like copying, printing, etc.,
OpenInMenu : But in this type this will list only the apps that are capable of opening the specified file type.

Note: If you just want to list the apps that are supported to open the specified file then go with the “OpenInMenu” option instead.

You can also notice that there are two ways in which the current method can be added, namely

– MenuFromRect and
– MenuFromBarButtonItem

MenuFromRect : Specifies a rect area from which the options menu will be presented.
MenuFromBarButtonItem : The options menu will be presented from the specified bar button item.

We can do lots more using UIDocumentInteractionController like making your application to support for opening specific kinds of files and list your application on the options menu whenever your application supporting file is opened anywhere in the device.

To instead present a menu that contains only a list of apps capable of opening the current document, the presentOpenInMenuFromRect:inView:animated: method instead.

Until Next Time……

iOS Developer,
Mallow Technologies.

Leave a Comment

Your email address will not be published. Required fields are marked *