Build Your First iOS Widget — Part 3
Making a user-configurable widget with SiriKit
Note: This is the third and final part of my 3-part widget tutorial. Make sure to complete part 1 and part 2 before continuing with this article.

Note: You’ll need Xcode 12 Beta 2 or higher to follow this tutorial.
So far, we have created a simple list-based app that displays different emoji along with their names, and allows you to view details about a specific emoji by tapping on it. Furthermore, we created a widget for our app–which can be small, medium, or large–and will display an emoji that is updated randomly every hour.
In this final part of the tutorial, we will use SiriKit to add a new widget that the user can configure to display an emoji of their choice.
User-Configurable Widgets
In some cases, we may want an app widget to display content chosen by the user. For example, a stock market app may let users choose a specific stock to display on a widget. Let’s go ahead and write some code to support adding a widget that displays an emoji of your choice.
Intent Definition
To get started, we first need to create a custom intent definition; this is what will allow us to define customizable properties for our widget (i.e. the emoji). To create an intent definition click on the File menu, select New File and select SiriKit Intent Definition File; name your file SelectEmojiIntent.
Now that the intent file is created, we need to define our intent. To do this, click on the ‘+’ icon on the bottom left of the intent file and select New Intent, give your intent the name SelectEmoji. Set the category to View; make sure that Intent is eligible for widgets is selected, and the other options are deselected.

Now that we’ve created our intent, we need to define the configurable properties that our intent will handle. We will define a new INObject type (intent object) called EmojiINO (emoji intent object). Click on the ‘+’ icon again, and select ‘New Type’, give it the name EmojiINO. You should see a new type created with the identifier and displayString properties.

Now go back to your SelectEmoji intent and add a new parameter called emoji, changing the type to EmojiINO. Make sure that ‘Options are provided dynamically’ is selected.

The EmojiINO type describes the emoji that a user can choose in a SelectEmojiIntent. Now we will write some code for providing the emoji options dynamically.
Intents Extension
To dynamically provide the list of supported emoji, we need to add an Intents Extension. Here, we will create an IntentHandler that will create a list of EmojiINO from all the emoji in our EmojiProvider. To add the intents extension choose File -> New -> Target… and select Intents Extension. Press Next and give a name to your extension (e.g. Emojibook Intent), make sure that None is selected as the starting point. Click Finish, and activate the Intents scheme if prompted by Xcode.
Navigate to the General tab of the Emojibook Intent target and add SelectEmojiIntent as one of the supported intents, like so:

Now go to the intent definition file, and make sure that it is a member of the Emojibook app, the Widget, and the Intents extension. Do the same for the EmojiDetails.swift and the EmojiProvider.swift files.

We can now go to IntentHandler.swift and implement the SelectEmojiIntentHandling protocol to provide the emoji options. Our IntentHandler.swift file will look like so:









