Getting Started with iOS App Extension Widget

AppExtension in iOS let you to have addition functionality and contents for your app which is beyond the scope of your application and make it available to users when they are interacting with the other apps.

For example, to let users to catch up on there favorite item in the app to be accessible by getting updates on the same without accessing the app, you can provide a Today widget that displaying favorite item updates in Notification Center. Or, to let users post to your social service from a web browser, you can provide a Share extension.

There Are Several Types of App Extensions which is well explained in this link.

LifeCycle of AppExtension

LifeCycle and environment of App and AppExtension are totally different. Activity of the extension fires only after when a user chooses it from an app’s UI or from a presented view controller. An app that enables user to choose an app extension is called a host app.

A host app defines the context provided to the extension and initiate the extension life cycle when it sends a request in response to a user action. An extension typically terminates soon after it completes the request it received from the host app.

The basic life cycle of an app extension

The basic life cycle of an app extension

For example, imagine that a user selects some content in an iOS host app, activates the Share button, and chooses an app extension from the sharing list to help them post that content to a social sharing website.

The host app responds to the user’s choice by issuing to the extension a request that contains the selected content. A generalized version of this situation is pictured in step 1 of above image.

In step 2 of above image, the system instantiates the app extension identified in the host app’s request and sets up a communication channel between them. The extension displays its view within the context of the host app and then uses the items it received in the host app’s request to perform its task (in this example, the extension receives the selected content).

In step 3 of above image, the user performs or cancels the task in the app extension and dismisses it. In response to this action, the extension completes the host app’s request by immediately performing the user’s task or, if necessary, initiating a background process to perform it.

The host app tears down the extension’s view and the user returns to their previous context within the host app. When the extension’s task is finished, whether immediately or later, a result may be returned to the host app.

Shortly after the app extension performs its task (or starts a background session to perform it), the system terminates the extension, as shown in step 4.

Some APIs Are Unavailable to App Extensions

  • Access a sharedApplication object, and so cannot use any of the methods on that object.
  • Use any API marked in header files with the NS_EXTENSION_UNAVAILABLE macro, or similar unavailability macro, or any API in an unavailable framework.
    For example, in iOS 8.0, the HealthKit framework and EventKit UI framework are unavailable to app extensions.
  • Access the camera or microphone on an iOS device.
  • Perform long-running background tasks(An app extension can initiate uploads or downloads using an NSURLSession object, with results of those operations reported to the containing app.)

Creating an Today’s Extension

As usual we are stating by Creating a new Xcode Project, In the welcome window as shown below.

Creating a new Xcode Project

Creating a new Xcode Project

Now Xcode opens a new window and displays a dialog in which you choose a template following these steps iOS > Application > Single View Application > next

Single View Application

Single View Application

In the dialog that appears, use the following values to name your app and choose additional options for your project:

  • Product Name: The name of your application. Lets keep it TodaysExtension. Xcode uses the product name you entered to name your project and the app.
  • Organization Name: The name of your organization or your own name. This is optional, hence you may leave this blank.
  • Organization Identifier: It is usually the reverse of your primary business domain. For example, if your business domain is stacktips.com then the organization identifier can be com.stacktips.
    Bundle Identifier: This value is automatically generated based on your product name and organization identifier.
  • Select Swift from the language drop down.
  • Devices: Select Universal. A Universal app is one that runs on both iPhone and iPad.
  • Use Core Data: Unselected.
  • Include Unit Tests: Unselected.
  • Include UI Tests: Unselected.
  • Click Next
Project Name Dialog

Project Name Dialog

Save the project into the desire location :

location to save your project and click Create

location to save your project and click Create

Now it’s time to create AppExtension by creating a new Target : File > New > Target

Creating New Target

Creating New Target

Next , Application Extension > Today Extension > Next

Selecting Today's Extension

Selecting Today’s Extension

Can set the Project Name as TodaysExtTarget

Setting Extension Target Project Name

Setting Extension Target Project Name

Activate the Extension scheme as shown below:

Activating Extension Scheme

Activating Extension Scheme

Enable the Todays Extension in Simulator by Clicking on Edit > + Button > Done as show below:

Enabling Today's Extension in Simulator

Enabling Today’s Extension in Simulator

Now , It’s time to Create a new URL Scheme by Selecting Project File > Selecting Target > Info > scroll down and select URL Types > Add Identifier and URL Schemes

Creating URL Scheme

Creating URL Scheme

Open Maininterface.storyboard and Drag and drop a Label from Object Library to Storyboard as show below:

Drag and Drop Label from Object Library to StoryBoard

Drag and Drop Label from Object Library to StoryBoard

Create a Outlet of the Label by Opening Associate Inspector from Storyboard window and control + Drag the Label , In viewDidLoad paste following snippet

 //Use tap gesture on label to launch app
labelText.userInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(TodayViewController.doLaunchApp))
labelText.addGestureRecognizer(tapGesture)

Add doLaunchApp function below the viewDidLoad as show below

func doLaunchApp(){
if let url = NSURL(string: "TodaysExtTarget://"){
self.extensionContext?.openURL(url, completionHandler: nil)
}
}

Uff finally we are done with the coding part, Run the app go to HomeScreen(cmd + shift + H) Tap on the Widget and it will take you to your App Initial screen.

You can get the source in the following link.

Satish Kumar

I am a self-driven software professional from Bangalore who live with a desire to set new examples, something that can create a unique signature of my own. I am a very friendly, fun loving person. I love to interact with people and make new friends who can bring difference in my life. I strive to become a better programmer.

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.