An Introduction to Extensions in iOS 9

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
An iOS 9 MapKit Flyover TutorialAn iOS 9 Today Extension Widget Tutorial


Purchase the fully updated iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook 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.




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 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 84-1).


Ios 8 today view.png

Figure 84-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 9 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 84-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.


Ios 8 share extension in view.png

Figure 84-2

Purchase the fully updated iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book

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


Ios 8 share view controller.png

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


Ios 8 action in view.png

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


Ios 8 photo editing extensions.png

Figure 84-5


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

Purchase the fully updated iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book

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.

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 84 6 listing a template for each of the extension types:


Xcode 7 extension targets.png

Figure 84-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 9 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. 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.


Purchase the fully updated iOS 10 / Swift 3 / Xcode 8 edition of this book in eBook ($19.99) or Print ($45.99) format.
iOS 10 App Development Essentials Print and eBook (ePub/PDF/Kindle) edition contains over 100 chapters. Learn more...

Buy eBook Buy Print Preview Book



PreviousTable of ContentsNext
An iOS 9 MapKit Flyover TutorialAn iOS 9 Today Extension Widget Tutorial