Tuesday, June 17, 2014

iOS App Extensions - In Detail

One of the very lucrative functionality with the extension is that user can provide notification centre widgets. A system area that support extension is called extension point. Each extension point provides usage policies and provides APIs that a developer can create extension in that area. 

Below are the extension points available in iOS 8.0 

Today (iOS and OS X) => Get a quick update or perform a quick task in the today view of notification centre. A Today extension is called a Widget. 
Share (iOS and OS X) => Post to a sharing website or share content with others
Action (iOS and OS X) => Manipulate or view content within the context of another app 
Photo Editing (iOS and OS X) => Edit photos or video within photos app
Finder (OS X) => Use remote storage files in OS X
Storage Provider (iOS) => Choose document from among the set of documents the current iOS app can access. 
Custom Keyboard (iOS) => Replace the iOS system keyboard with a custom keyboard for use in all apps 

it is upto the application to choose the right extension point. 

An app extension is a separate binary that executes independent of the app. One can create a new extension by defining a new target to an app. As with any target, an extension target specifies settings and files that combine to build a product within your app project. One can add multiple extensions to the target app. 
an app is called a "containing app" if it contains one or multiple extensions

When user installs the containing app, the extension also get installed

After installing an extension, user must take action to enable it. Often user can enable extension within the context of their current task. For e,g. user can edit notification centre to enable a Today extension. 

A developer should provide and icon for the app extension. Usually this will be the app icon itself. 


Below images gives overall life cycle of an app extension 



Below image gives an idea of communication between host app, extension and containing app. 

Simple Communication

Detailed communication 

Creating an Extension: 

Xcode supplies several extension templates. Once user selects and extension template, project will be added with the .appex extension file to the project. 

Cross-secting Default Extension Template
each extension template contains a property list file (that is info.plist file), a view controller class and a default user interface, all of which are defined by the extension point. The default view controller class (or principal class) can contains stubs for the extension point methods one should implement. 

the .plsit file should contain at least NSExtension key and a dictionary of keys and values each extension point specifies. For e.g. NSEXtensionPointIDentifier is a required key that is a reverse DNS name for e.g. com.livingmobile.myextension. 

some of the common keys are: 

NSExtensionAttributes: A dictionary of extension point specific attributes. such as PHSupportMediaTypes for a photo editing extension. 
NSExtensionPrincipleClass: The name of the principle extension point class. 
NSExtensionMainStoryBoard: Only for iOS. The default storyboard file for the extension, usually named as MainInterface. 

As soon as the host app issues the request (typically by calling beginRequestWithExtensionContext: method), your extension can use the extensionContext property on its principal view controller to get the context. The extension can examine the items inside the extension context and get items within it. Normally it will be good to get the extension context in the principal controllers loadView method so that one can display information in the view. To get the extension context, one can do the coding like below. 

NSExtensionContext *extContext = [self extensionContext];  

Once get the extension context, inputItems method can be invoked to get the individual extension item elements. Each ExtensionItem object contains a number of properties that describe aspects of an item such as its title, context text, attachments and user info. 

the attachments property contains an array of media data that is associated with an item. For example, in an item associated with a sharing context, the attachment property may contains a representation of webpage a use wants to share. After user work with extension using the input items, extension can give option to the user to cancel or complete the task. Depending on users choice, extension can call completeREquestREturningItems: expirationHandler:completion: optionally returning items to the host app or cancelRequestWithError:, returning an error code. We can use the NSURLSession to continue doing the potentially lengthy operation in the background such as uploading an image. 

Optimize Efficiency and Performance. 
Memory limits for running extensions are significantly lower than the memory limit imposed on a foreground app. Some extensions may have lower memory than others, for e.g. Today widgets. 

the extension doesn't own the main run loop. If the app blocks the main run loop, then it can give a bad impression in another extension or an app. 
Debugging an extension is similar to how the regular app is debugged. We can place breakpoints. It is advisable to have the instruments run on the extensions. 
An extension can be shipped only by using the containing app. 


No comments:

Post a Comment