Exposing an App’s Functionality through AppIntent

Use AppIntent to analyse the quality of your ios app

Overview of AppIntent

iOS 16 brings a lot of new features and frameworks, one of them being a brand new framework called AppIntent. This framework allows us to expose any capabilities or features to the system that will be captured by the Shortcut App and Siri.

Use app intents to express your app’s capabilities to the system. An app intent includes the code you need to perform an action and describes the data you require from the system. The system exposes your actions directly from the Shortcuts app and indirectly through natural language commands spoken to Siri.

To define an action, create a type that adopts the AppIntent protocol, or a related protocol that provides the specific behaviour you need. Annotate any key properties with the @Parameter property wrapper to let the system know you need the associated information to perform the action.

Let’s create a simple app to demonstrate the usage of AppIntent called Sport Log. This little app’s purpose is to track your sports activity every day

Sport app demonstration with AppIntent

There are 6 types of activities that users can record in this app, so we have to create an enum for these activities.

Code example

This enum implements 2 protocols, `String` for the enum type, `CaseIterable` for the sake of iterable. We also provide the extension of this enum for the title and the icon

Extension example

If you look closely at the bottom tab, there are 2 tabs provided, the list of Sport Activity and the My Progress tab. The purpose of the second tab  is to display the user’s progress activity result 

User’s progress activity result

If you were to release this app to the public, I believe that users would open this page frequently as they would want to see their activity progress daily, so it’s not a bad idea to create a shortcut for the user to directly see the content of this page via a shortcut. That’s where the AppIntent Framework comes in handy.

As the Apple WWDC document says how to implement this AppIntent, we just need to create a simple `struct` that implements the `AppIntent` protocol. So that’s what we’re going to do.

`AppIntent` protocol example

The `AppIntent` protocols required us to define a single property: the `title`. This property will be used as a title for the action that will be listed in the Shortcut’s action list. The `perform()` function with the `MainActor` decorator will be called once the shortcut is executed. In this function, we will present the `ProgressView` view to the user.

*. MainActor is a new attribute introduced in Swift 5.5 as a global actor providing an executor which performs its tasks on the main thread.

Once we create this, the intent will be installed automatically in the Shortcut App once the user installs the app. You can check it in the shortcut app. You need to create a new shortcut, and then add a new action, choose the SportLog app, and then the intent will pop up.

SportLog App intent with Shortcut app

Select the intent and press Done, the shortcut will be created for you. If you click the shortcut, it will present you with a view that we earlier provided in the `perform` function.

User’s progress activity result with Shortcut app

Because it’s in the Shortcut app, you can pin this shortcut to the desktop, and it will give the user a simple way to monitor their progress without opening the app. Neat.

Parameterized Shortcut

The AppIntent protocol has a lot of optional properties in the form of a decorator, one of them is `Parameter`. Just like the name, its purpose is to give the parameter for the intent. In real life, for the sake of simplicity, you may need to let users open the page they want directly to start the activity tracker. That’s why we need to provide parameters for the intent. So, let’s create another intent.

Parameter example

As you can see, there are new codes coming in. The `openAppWhenRun` is the property to tell the system to open the related app when the user clicks the shortcut. While the `Parameter` decorator, followed by the variable `activity`, is the parameter that we talked about earlier. This parameter supports a variety of data types, from a primitive data type, like `int`, `string`, and `boolean`, to even user-defined data types like `enum`. You may notice that the variable type is `SportActivity`, it is the enum that we created before.

To expose our enum to the AppIntent, we have to implement the `AppEnum` protocol in our enum.

Protocol example

There are 2 properties that need to be defined: `typeDisplayName` and `caseDisplayRepresentations`. Both of them are needed to present the options to the user. 

Related sport activity result

Try to create and click it, the app will open and a related activity page will be presented.

Automatic Shortcut

It’s quite tricky to create the shortcut before AppIntent is introduced. With this framework, you can create the shortcut programmatically, it will be listed in the Shortcut App once you installed the app. Just like AppIntent, we create it with a simple struct that implements `AppShortcutsProvider`.

Struct example

There is one property that you have to define, and it returns the `AppShortcut` with 2 parameters, `intent` for the AppIntent and `phrases` for the `Siri` phrase command. Once you run the app, you will see the shortcut has been created for you automatically inside the Shortcut App.

Automatically create shortcut inside the Shortcut app

There are still a lot of features you can explore about the AppIntent that you can see in the WWDC Video related to this framework. 

You can find the sample project on this GitHub project.


Dive into AppIntents
Implement App Shortcuts with AppIntents
AppIntent Documentation

Showing responses for

Leave a Reply

Your email address will not be published.

More Articles

let's talk illustration

Loving what you're seeing so far?

It doesn’t have to be a project. Questions or love letters are fine. Drop us a line

Let's talk arrow right