An Introduction to Extensions in iOS 8

From Techotopia
Revision as of 19:25, 8 November 2016 by Neil (Talk | contribs)

Jump to: navigation, search
PreviousTable of ContentsNext
Using MKDirections to get iOS 8 Map Directions and RoutesAn iOS 8 Today Extension Widget Tutorial


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book


Extensions are a feature originally 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.


Contents


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. With the exception of Message App extensions, 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 is constantly evolving, though the key types 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 83-1).


[[Image:]]

Figure 83-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 10 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 83-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:]]

Figure 83-2


Share extensions can make use of the SLComposeServiceViewController class to implement the interface for posting content. This displays a view (Figure 83-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:]]

Figure 83-3


The actual mechanics of posting the content will be dependent on the way in which the target platform works.

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 83-4, for example, shows an example Action extension named “Translator” in the activity view controller of the iOS Notes app.


[[Image:]]

Figure 83-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 10 Action Extension and Receiving Data from an iOS 10 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 83-5 shows the Photos app displaying two Photo Editing extension options:


[[Image:]]

Figure 83-5


Photo Editing Extensions are covered in detail in the chapter entitled Creating an iOS 10 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.

Audio Unit Extension

Audio Unit Extensions allow sound effects, virtual instruments and other sound based capabilities to be made available to other audio oriented host apps such as GarageBand.

Shared Links Extension

The Shared Links Extension provides a mechanism for an iOS app to store URL links in the Safari browser shared links list.

Content Blocking Extension

Content Blocking allows extensions to be added to the Safari browser to block certain types of content from appearing when users are browsing the web. This is typically used to create ad blocking solutions.

Sticker Pack Extension

An extension to the built-in iOS Messages App that allows packs of additional images to be provided for inclusion in message content.

iMessage Extension

Allows interactive content to be integrated into the Messages app. This can range from custom user interfaces to interactive games. iMessage extensions are covered in the chapters entitled An Introduction to Building iOS 10 Message Apps and An iOS 10 Interactive Message App Tutorial.

Intents Extension

Used when integrating an app with the SiriKit framework, these extensions are used to define the actions to be performed in response to voice commands using Siri. SiriKit integration and intents extensions are covered in the An Introduction to SiriKit, An iOS 10 Example SiriKit Messaging Extension and An iOS 10 SiriKit Photo Search Tutorial chapters of this book.

Creating Extensions

By far the easiest approach to developing extensions is to use the extension templates provided by Xcode. 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 83 6 listing a template for each of the extension types:


[[Image:]]

Figure 83-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, Action and Message app 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 10 currently supports a variety of 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. 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.


Learn SwiftUI and take your iOS Development to the Next Level
SwiftUI Essentials – iOS 16 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book



PreviousTable of ContentsNext
Using MKDirections to get iOS 8 Map Directions and RoutesAn iOS 8 Today Extension Widget Tutorial