Introduction to iOS Today Extensions

Emrah
inventiv
Published in
4 min readFeb 20, 2020

I’ll cover the iOS Today Extensions in this article.

An image about Apple.

Today Extensions in iOS provides an extendible feature in the Widget of the iOS. So you can handle this extension how you wish. Let’s start.

Creating Extension

In File -> New -> Target;

Window for Targets

After you select Today Extension name it SimpleTodayExtension to follow this article with me.

Well Done. You’ve created your Today Extension.

If you debug your Today Extension, you have to select Today Extension target before the run otherwise you’ll better to use Application’s target. Bothside you’ll see the changes on codebase & effect on them.

Run it & In NotificationCenter you’ve to add SimpleTodayExtension as Widget.

Result of Today Extension.

This is how your extension will seem at the first run of Application.

Main Interface

There’s an interface for Today Extension. We’ll change the content of it. If you’re familiar with Storyboard you’ll handle easily. After clicking the MainInterface.storyboard file in Project Structure, you’ll see this screen. This is all the visual playground of Today Extension.

MainInterface of Today Extension.

Now it’s time to select the view of TodayViewController. Then in Size Inspector set it’s height to 200. Then the Hello World button will have appeared.

I am going to delete this UILabel then add a new one manually. The name of the label will be priceStatusLabel. Then set its Horizontally in Container & Vertically in Container constraint equal to the view of the controller of the extension. And I’ll connect it with the controller of the Today Extension.

Code For TodayViewController

Well done if you did all of the steps for it. After you run the App or the Extension center UILabel will be shown at Widget.

preferredContentSize

preferredContentSize variable is specifies the content size of TodayExtension’s UIView. There’s no matter to set width with this variable because the width of the Extension’s View is constantly equal with Device’s width. So for changing height, this is the best way.

Make Today Extension Screen Expandable

If you wanna your widget expandable you’ve to set it’s expandable

Expand the codeview.

You’ve to set extensionContext’s widgetLargetsAvailableDisplayMode to expanded so when you expand it set the expand mode & its height.

We made expandable.

Yay!. It’s expandable now. You can set your UILayout & Items with control if its expanded or compact.

What is the extensionContext really?

extensionContext is Optional variable of the UIViewController. When you run Application Target the extensionContext will be nil. But other-side when you run the directly extension, it will not be nil.

For example, when you open a URL or call something is about UIApplication, you cannot call it from Extension because it cannot call the UIApplication object for using. But you can achieve the operations in Extension with extensionContext.

The usage of the extensionContext.

widgetPerformUpdate

This function is called when the extension wake up with viewDidLoad() and check for if there’s any change in state of the extension.

You better to avoid UI process in the widgetPerformUpdate. Because when app is running and widget is active, it can use main thread in this function. So this can be not good for the app performance. You better do UI things in the viewDidAppear method. But it’s best way to handle background Network Request’s.

There’s 3 type of update result. If you gonna refresh the content of the Today Extension you must trigger .newData and if there’s no change go with .noData If there’s a error with anything you can use .failed

Navigate to Application

Assume that you’ve a button and after clicking it you’ve to open your application. So let’s create a custom URL Scheme.

In Application Scheme -> Info -> URL Types

add new URL Scheme like below.

Set your identifier correctly and append .url & set your URL Scheme name to a specific name like your app name. We can share values in same UserDefaults via group-id and also we can send parameters when navigation from Extension to the Application.

So connect your UIButton and then add #selector for informButtonClicked function.

In the function, we can share values like amountValue with shared UserDefaults as seen above. This is the one of the good advantages of using TodayExtension. Another advantages of the TodayExtension we can easily use to navigate to the Application with parameters.

As you see there’s a parameter with named username=simpleUsername. After you clicking the button in TodayExtension, the application will be opened.

Yay!

You can handle the parameters and change your application’s structure & diagram via these given parameters as like function above.

You can use the shared group values with using standart functions of UserDefaults as below.

Get amountValue in the app.

You can easily manipulate all of the shared data, parameters. So I am happy to describe most basic ways of the TodayExtension 🙌 You can also ask question about the article and TodayExtension structure.

--

--