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.

Image for post

Creating the SelectEmoji intent.

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](https://developer.apple.com/documentation/sirikit/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.

Image for post

EmojiINO type.

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.

Image for post

#xcode #programming #ios #swift #software-engineering

Build Your First iOS Widget — Part 3
1.60 GEEK