Did you know that our iOS application allows you to easily import documents from other iOS applications? Document provider and share extensions do just that, and much more.
Read on what iOS extensions are, why they are built and how you can use document provider and share extensions in your daily tasks.
From the beginning iOS platform was designed to be very secure and to protect user data. That’s why Apple created strict limitations for applications, which run on iOS environment; every application must be signed and verified by Apple and every application is sandboxed. The sandboxing means that every application live it its own environment (sandbox) and it can read and write data only there. An application cannot read data from other application directly nor can it write into it.
Before iOS8 two applications could exchange data only via “Open In:” functionality. Every time when you select a document and choose to open it in another application, iOS would create new copy of the document and transfer it into the application’s sandbox. You would make some changes there and you should do “Open In:” again to return the changed document into the original application. And then you’ll have 2 documents; one without applied changes and the other with changes.
The new way in which two applications can communicate, introduced in iOS8, is via iOS extensions.
The iOS extensions are, like it’s own name says, extensions for main applications. They extend applications functionality and allow you to indirectly interact with other applications. For example, the share extension allows you to pick a photo from your photo library and to share it on your favorite social network or cloud storage. Every extension is actually a mini application which does one task, for example document sharing or photo editing and that’s it. Extensions can run only on iOS8 and above and an extension cannot run without it’s main application.
The application in which an extension is invoked is called host application, and application which holds the extension (the application which brought the extension with it) is called containing application. Every extension can interact only with its host application and it’s “alive” only when user invokes it. When extension finishes its task, it will become inactive. There’s no hidden process that keeps an extension alive while it’s not used.
There are 6 different types of extensions: today widget, share, action, photo editing, document provider and custom keyboard. We’ll talk here only about 2 of them, share and document extensions.
The share extension allows you to share content between applications. In fact, this functionality was introduced in iOS5 but it allowed you only to share content and images with Twitter. iOS6 gives you possibility to do the same with Facebook and iOS8 allowed that to third party applications.
Almost every application has it’s own share extension. Don’t be surprised if you didn’t know that, because when you install an application there are no indicators, visible at first sight, that the application contains extensions. The share extensions are usually hidden under the universal symbol for sharing.
For example, when you open Photos application and select an image, you’ll notice at the bottom left corner the share icon. When you press it the dialog will show similar to this.
At the top of the dialog are share and at the bottom are action extensions. If an application contains the share extension it won’t show in the list automatically because you have to give explicit permission to every extension you want to use. If you press “More” button in the list of extensions you’ll see them all. I’m always surprised when I see how many share extensions exists for which I didn’t know.
Have in mind that two applications can have share extension that behaves totally different. The extension developer have to configure what type of content the extension can handle and what’s the minimum and maximum number of items that can be shared at a time. CenterDevice can share up to 10 images at a time and if you select 11th the extension will hide from the extensions list and it’ll become visible again when you unselect the 11th image. At the moment you can share only images via CenterDevice share extension, but there’s a plan to implement more type of documents in the near future.
Document provider extension
The document provider extension is a new type of extension introduced in iOS8. Actually, it’s a combination of 2 separate extensions:
- Document picker
- File provider
The document picker is an UI component that can be presented when another application launches an iCloud file picker.
The file provider is a non-UI component that coordinates reading/writing into a file and manages access to a file between host and containing application.
The document picker allows you to pick a file and to do one of 4 supported operations:
So how do those operations work?
When you open a document provider extension, you’ll see first the document picker. The navigation bar of the document picker extension contains “Locations“ and “Done” buttons.
The „Done“ button closes the document picker extension, and the „Locations“ button provides you a list of applications installed on your iOS device that have the document provider extension. Those applications actually allowed public access to documents in their sandbox.
To export a document into another application, you’ll pick from the Locations list an application in which you want to export the selected document. When you pick an application, then a document extension screen of the application will show. For example, when you pick CenterDevice application from the „Locations“ list, CenterDevice’s collections screen will show.
Tapping on the CenterDevice’s upload button will invoke that the document extension creates a copy of selected document and transfer it into CenterDevice’s sandbox.
The import functionality does the same, just in the opposite direction. It allows you to import a copy of a document of other application into containing application.
Move functionality works similar like export; the document provider copies selected document into other application’s sandbox, but remove it from the original application.
Open functionality is the most interesting. When you open a document via document extension, the file provider will create a copy of the file, but the copy won’t be transferred into the host’s sandbox. It will stay in the file provider extension and the host application will get a reference to it. The changes in the file will be made directly into file provider and saved back to the original file when you close the file. In my opinion, this is the most powerful functionality of document extension.
If an application has the document provider extension, it doesn’t mean that it provides all 4 operations. Some applications provide only document import and some of them only the document export functionality. The best example for it is the Microsoft Office apps (Word, Excel and Power Point) for iPad and iPhone. In those applications you can only open a document from iCloud for editing, but you cannot create a new document and export it to iCloud. Microsoft allows you to export document only in applications that belongs to the Microsoft Storage Partner Program. More about that you can read it here
Developers can limit file types that can be accessed by the document extension. Unsupported file types are disabled and grayed out, so you won’t be able to select them.
Unfortunately there’s no universal icon which tells you that the document provider will open when you tap on it. It’s usually cloud icon, but it need not be. Here are few examples.
Finally we can conclude that the document provider extension is very powerful tool. Its move functionality allows you to move a document from one application into another and open functionality allows you to edit a document into another application easier then ever. Changes will be applied in original document without making unnecessary copies or versions of the file.
But, there are only so many applications with document provider extension, and maybe just few of them supports direct file editing with open functionality. Direct file editing require more careful coding and dealing with concurrent access to a file. iOS9 and split view on iPad Air2 gives one more level of complexity. You could open a document in one application and, at the same time, edit the document via document extension. We have yet to see how applications will cope with that.