Today Extensions Tutorial in iOS8 with Swift

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 built in iOS 8.1 and Xcode 6.1.

Open Xcode and create a new Single View Application. For product name, use IOS8SwiftTodayExtensionTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

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". Also, give the labels a white text color. The Storyboard should look like this.

Select the iOS Version Label and choose the Pin Button to the lower-right of the Storyboard(the 2nd button on the left). Add the following constraints.

Repeat this step for the right Label.

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

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

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

The NSProcesInfo has an operatingSystemVersion which return the current OS, divided by a major, minor and a patch version. These versions will be assigned to the label's 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 IOS8SwiftTodayExtensionTutorial at the ioscreator repository on Github.