Changes

Jump to: navigation, search

An Introduction to Extensions in iOS 8

11,092 bytes added, 15:42, 9 December 2014
New page: <table border="0" cellspacing="0" width="100%"> <tr> <td width="20%">Previous<td align="center">[[iOS 8 App Development Essent...
<table border="0" cellspacing="0" width="100%">
<tr>
<td width="20%">[[Using MKDirections to get iOS 8 Map Directions and Routes|Previous]]<td align="center">[[iOS 8 App Development Essentials|Table of Contents]]<td width="20%" align="right">[[An iOS 8 Today Extension Widget Tutorial|Next]]</td>
<tr>
<td width="20%">Using MKDirections to get iOS 8 Map Directions and Routes<td align="center"><td width="20%" align="right">An iOS 8 Today Extension Widget Tutorial</td>
</table>
<hr>


<google>BUY_IOS8</google>


Extensions are a new feature introduced as part of the iOS 8 release designed to allow certain capabilities of an application to be made available for use within other applications. The developer of a photo editing application might, for example, have devised some unique image filtering capabilities and decide that those features would be particularly useful to users of the iOS Photos app. To achieve this, the developer would implement these features in a Photo Editing extension which would then appear as an option to users when editing an image within the Photos app.

Extensions fall into a variety of different categories and a number of rules and guidelines must be followed in the implementation process. While subsequent chapters will cover in detail the creation of extensions of various types, this chapter is intended to serve as a general overview and introduction to the subject of extensions in iOS.

== iOS Extensions – An Overview ==

The sole purpose of an extension is to make a specific feature of an existing application available for access within other applications. Extensions are separate executable binaries that run independently of the corresponding application. Although extensions take the form of an individual binary, they must be supplied and installed as part of an application bundle. The application with which an extension is bundled is referred to as the containing app. The containing app must provide useful functionality and must not be an empty application provided solely for the purpose of delivering an extension to the user.

Once an extension has been installed, it will be accessible from other applications through a number of different techniques depending on the type of the extension. The application from which an extension is launched and used is referred to as a host app.

An application that translates text to a foreign language might, for example, include an extension which can be used to translate the text displayed by a host app. In such a scenario, the user would access the extension via the Share button in the user interface of the host app and the extension would display a view controller displaying the translated text. On dismissing the extension, the user is returned to the host app.

== Extension Types ==

iOS supports a number of different extension types dictated by extension points. An extension point is an area of the iOS operating system which has been opened up to allow extensions to be implemented. When developing an extension, it is important to select the extension point that is most appropriate to the features of the extension. The extension types supported by iOS can be summarized as follows:

=== Today Extension ===

The Today extension point allows extensions to be made available within the Today view of the iOS Notification Center (the panel that appears when making a swiping motion downward from the top of the device display as shown in Figure 79-1).


[[Image:ios_8_today_view.png]]

Figure 79-1


By default, the Today view displays information such as calendar appointments for the current day and prevailing stock price information.
Today extensions take the form of widgets that display information to the user when the Today view is displayed. Today extensions are covered in detail in the chapter entitled [[An iOS 8 Today Extension Widget Tutorial]].

=== Share Extension ===

Share extensions provide a quick access mechanism for sharing content such as images, videos, text and web sites within a host app with social network sites or content sharing services. It is important to understand that Apple does not expect developers to write Share extensions designed to post content to destinations such as Facebook or Twitter (such sharing options are already built into iOS) but rather as a mechanism to make sharing easier for developers hosting their own sharing and social sites. Share extensions appear within the activity view controller panel which is displayed when the user taps the Share button from within a host app.

Figure 79-2, for example, shows a Share extension named “Share It” listed alongside the built-in Twitter, Facebook and Flickr share options within the activity view controller.


[[Image:ios_8_share_extension_in_view.png]]

Figure 79-2


Share extensions can make use of the SLComposeServiceViewController class to implement the interface for posting content. This displays a view (Figure 79 3) containing a preview of the information to be posted and provides the ability to modify the content prior to posting it. For more complex requirements, a custom user interface can be designed using Interface Builder.


[[Image:ios_8_share_view_controller.png]]

Figure 79-3


The actual mechanics of posting the content will be dependent on the way in which the target platform works.
<google>BUY_IOS8</google>
=== Action Extension ===

The Action extension point enables extensions to be created that fall into the Action category. Action extensions allow the content within a host app to be transformed or viewed in a different way. As with Share extensions, Action extensions are accessed from the activity view controller via the Share button. Figure 79-4, for example, shows an example Action extension named “Translator” in the activity view controller of the iOS Notes app.


[[Image:ios_8_action_in_view.png]]

Figure 79-4


Action extensions are context sensitive in that they only appear as an option when the type of content in the host app matches one of the content types for which the extension has declared support. An Action extension that works with images, for example, will not appear in the activity view controller panel for a host app that is displaying text based content.

Action extensions are covered in detail in the chapters entitled [[Creating an iOS 8 Action Extension]] and [[Receiving Data from an iOS 8 Action Extension]].

=== Photo Editing Extension ===

The Photo Editing extension point allows the photo editing capabilities of an application to be accessed from within the built-in iOS Photos app. Photo Editing extensions are displayed when the user selects an image in the Photos app, chooses the edit option and taps on the button in the top left hand corner of the Photo editing screen. Figure 79-5 shows the Photos app displaying two Photo Editing extension options:


[[Image:ios_8_photo_editing_extensions.png]]

Figure 79-5


Photo Editing Extensions are covered in detail in the chapter entitled [[Creating an iOS 8 Photo Editing Extension]].

=== Document Provider Extension ===

The Document Provider extension makes it possible for a containing app to act as a document repository for other applications running on the device. Depending on the level of support implemented in the extension, host apps can import, export, open, and move documents to and from the storage repository provided by the containing app. In most cases the storage repository represented by the containing app will be a third-party cloud storage service providing an alternative to Apple’s iCloud service.

A Document Provider extension consists of a Document Picker View Controller extension and an optional File Provider extension. The Document Picker View Controller extension provides a user interface for the extension allowing the user to browse and select the documents available for the Document Provider extension.

The optional File Provider extension provides the host app with access to the documents residing outside of the app’s sandbox and is necessary if the extension is to support move and open operations on the documents stored via the containing app.

=== Custom Keyboard Extension ===

The Custom Keyboard Extension, as the name suggests, provides the ability to create and install custom keyboards onto iOS devices. Keyboards developed using the Custom Keyboard extension point are available to be used by all applications on the device and, once installed, are selected from within the keyboard settings section of the Settings app on the device.

== Creating Extensions ==

By far the easiest approach to developing extensions is to use the extension templates provided by Xcode. As previously discussed, extensions must be associated with a containing app. Once the project for a containing app is loaded into Xcode, extensions can be added in the form of new targets by selecting the File -> New -> Targets… menu option. This will display the panel shown in Figure 79 6 listing a template for each of the extension types:


[[Image:xcode_6_extension_template_selection.png]]

Figure 79-6


Once an extension template has been selected, simply click on Next to name and create the template. Once the extension has been created from the template, the steps to implement the extension will differ depending on the type of extension selected. The next few chapters will cover in detail the steps involved in implementing Today, Photo Editing and Action extensions.

== Summary ==

Extensions in iOS provide a way for narrowly defined areas of functionality of one application to be made available from within other applications. iOS 8 currently supports Action, Share, Photo Editing, Document Provider, Today and Custom Keyboard extension types. It is important when developing extensions to select the most appropriate extension point before beginning development work and also to be aware that some application features may not be appropriate candidates to be placed into an extension.

Although extensions run as separate independent binaries, they are only able to be installed as part of an app bundle. The app with which an extension is bundled is called a containing app. Apple requires that containing apps provide useful functionality to the user and must not be empty apps intended solely as a delivery platform for an extension. An app from which an extension is launched is referred to as a host app.

Having covered the basics of extensions in this chapter, subsequent chapters will focus in detail on the more commonly used extension types.


<google>BUY_IOS8</google>


<hr>
<table border="0" cellspacing="0" width="100%">
<tr>
<td width="20%">[[Using MKDirections to get iOS 8 Map Directions and Routes|Previous]]<td align="center">[[iOS 8 App Development Essentials|Table of Contents]]<td width="20%" align="right">[[An iOS 8 Today Extension Widget Tutorial|Next]]</td>
<tr>
<td width="20%">Using MKDirections to get iOS 8 Map Directions and Routes<td align="center"><td width="20%" align="right">An iOS 8 Today Extension Widget Tutorial</td>
</table>

Navigation menu