Today Extension iOS Tutorial

App extensions were introduced in iOS to extend the reach of your app to some parts of the OS. The Today Extensions provides a quick widget in the Notification Center. In this tutorial we will create a Today Extension, which will show the current iOS version of the device. This tutorial is made with Xcode 9 and built for iOS 11.

Open Xcode and create a new Single View App.

For product name, use IOS11TodayExtensionTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

Open the File menu and choose New > Target -> Application Extension ->Today Extension

Click Next, give your widget a name and click Finish to create the new target. Xcode has created a new scheme for you and it will ask you to activate it for you. Click Activate to continue.

The new target has added a folder TodayExtension to the project containing a ViewController file and a Storyboard. Go to the Storyboard and delete the Hello World label. Add two new Labels and position them to the far left and the far right. Give the left label a title of "iOS Version".

Select the iOS Version Label and choose the "Add New Constraints" Button to the lower-right of the Storyboard(the 4th button on the left). Add the following constraints.

Repeat this step for the right Label.

The Storyboard should look like this.

The right label needs to display the current version, so first create an outlet. Select the Assistant Editor and make sure the TodayViewController.swift file is visible. Ctrl and drag from the right Label to the TodayViewController class  and create the following Outlet.

Go to the TodayViewController.swift file and change the viewDidLoad method to

override func viewDidLoad() {
    super.viewDidLoad()
    let os = ProcessInfo().operatingSystemVersion
    let version = "\(os.majorVersion).\(os.minorVersion).\(os.patchVersion)"
      
    versionLabel.text = version
}

The ProcessInfo class has an operatingSystemVersion property which return the current OS version, divided by a major, minor and a patch version. These versions will be assigned to the versionLabel text property. Change the Target to TodayExtension and Build and Run the project, In the Today's Notification section the Extension will be displayed.

You can download the source code of the IOS11TodayExtensionTutorial at the ioscreator repository on Github.