Extensions let iOS 8 apps extend functionality into the operating system. Cory Bohon shows you how easy extensions are to implement.
Developers have wanted a way to extend their applications into iOS, and now that's finally possible. In iOS 8, extensions run as a separate target from your app and can extend iOS in a number of ways, including through the Notifications Center's Today view.
What is an extension?
An extension is another target that is contained inside your existing applications. Both the extension and the application must be signed with your developer certificate and profile, and contain different bundle identifiers for the base application and the extension targets. Because your application isn't in control of the the extension's presentation, if your host application (i.e., the application interacting with the extension) crashes, it cannot affect your application's extension and vice versa.
The types of extensions
Xcode 6 displays all of the extensions types when adding a new target.
An action extension provides an action or a service to a host application by transforming passed in data into another format, or by doing some other task. An example of this extension type is the 1Password extension in iOS 8 that can be shown from Safari and provide access to your usernames and passwords from the base 1Password application.
Action extensions are available in a wide variety of application and data types, though you're going to commonly see them used in Safari. Action extensions appear in the second row of icons in the Share Sheet in a host application.
Custom keyboards, one of the most anticipated features in iOS 8, allow your base application to install an extension that can be used in almost any application in the place of a keyboard when a user edits a text field or text view.
Note: Custom keyboards can have network access, but that access can only be allowed if the user has enabled Full Access for your keyboard. You should always tell the user exactly what you are doing with the data gathered by a networked keyboard.
A document provider allows a host application presenting the extension to pull data or share data out to an application. Document providers could become the next big thing in iOS by allowing users to pull in documents from Dropbox and other cloud services to edit in applications like Pages, Numbers, and Keynote. In addition to pulling in documents from your application, the host application can also save documents back out to your application through the document provider.
A document provider extension will appear in the Share/Save Sheet of a host application.
There are a ton of photo editing applications available in iOS, but it's always a pain to open the application, load a photo, perform an edit, and then save the photo back to the Photos application in iOS. This extension enables users to do these tasks more efficiently.
With a photo editing extension, your application can advertise as a photo editing application, and it will appear when a user opens the share sheet for a photo in the Photos application. When selected, your extension can spring to life, providing your application to edit or otherwise change the photo that was passed in without the user having to leave the Photos app and open your individual application. This also works in other host applications that use the standard Quick Look view controller for displaying images.
If your application allows a user to post a photo, video, or other document type to your app or service, then you should be implementing a share extension. A share extension appears in the Share Sheet of a compatible application, offering a photo, video, or other document type so the app can share it with an online service, social network, or other share-style application. You can write your own UI to display to the user when sharing (such as entering text), though Apple includes the ability to reuse a TweetSheet style view that lets the user specify some text when sharing the document type.
The Notification Center is a great place to see all of your recent notifications at a glance. In iOS 8 and OS X Yosemite, Apple created a new Today View that appears in the Notification Center, and offers tidbits of information (such as upcoming appointments), and is a place that developers can extend their application support using extensions. You can take advantage of this space to advertise your application's data to users. An example of this extension is OmniFocus, which has built a Today extension that displays upcoming tasks from the base application.
Don't be an obnoxious developer who only writes a Today extension with a button to launch the base application. You want to ensure that your Today widget is providing a valuable service to users.
Caveats to implementing extensions
- Extensions cannot, by default, share data between the base application and the extension. (My next article about extensions will cover more about sharing data between your applications.)
- Network requests need to be handled through the iOS background network request operations, since your extension isn't guaranteed to be on-screen for the duration of the network request, and you should try to load cached data where possible when presenting your extension UI.
- Standard keyboard extensions don't allow for network requests, unless the user has enabled Full Access in the keyboard options.
- Keyboard extensions will be disabled when entering passwords into secure text fields, and the user will be defaulted to the standard iOS keyboard.
- Some extension types do not present a UI, and some have the option to not present a UI when running the extension.
Extensions can run on iOS and OS X, but due to platform differences, you will need to port and rework parts of the UI and experience for one platform or the other -- they cannot be directly translated from one platform to the other.
In the next installment in this series, we'll go into detail about how to create an extension inside your existing application using the built-in tools in Xcode. We'll build out a simple Today extension to show how easy it is to implement extensions.