Adding support for shortcuts can be a great way to enhance your app and integrate it seamlessly into the iOS ecosystem. Shortcuts allow users to interact with your app without having to navigate through specific screens. They can be executed through voice commands, text inputs, or even Spotlight search. Users can also create workflows by combining multiple shortcuts to accomplish multiple tasks at once.
In the past, adding shortcuts to your app and making them accessible to the system was a complex and time-consuming process. You had to use visual editors and map code files to Intent Definition files. However, with the introduction of iOS 16, Apple has revamped the process using the new App Intents framework. Now, you can create shortcuts directly in Swift, without the need for additional steps or code generation. These shortcuts are immediately available to users through Siri, the Shortcuts app, and Spotlight.
To get started, download the starter project and open it in Xcode. The project is called BreakLogger and it allows users to record break times throughout the day. Build and run the app, and you’ll see an empty break list. Tap the “Add” button to record a break. You can select a break duration from the options provided. After adding a break, it will appear in the break list.
Currently, the only way to log a break is through the app itself. However, iOS offers other ways to interact with apps in its ecosystem. Users might want to log a break as part of a larger workflow or ask Siri to log a break without opening the app. By integrating the App Intents framework into your app, you can enable these use cases.
To define your first app intent, right-click on the Source group in your project and create a new group called “Intents”. Inside this group, create a new Swift file named “LogBreakIntent.swift”. In this file, create a struct called “LogBreakIntent” that conforms to the AppIntent protocol. This struct will represent your shortcut. Add a static property called “title” to provide a localized title for your shortcut. Then, implement the “perform()” function to perform the desired action when the shortcut is executed. In this case, it logs a 15-minute break using the LoggerManager and returns a dialog message indicating that the break has been logged.
With the LogBreakIntent defined, users can create a shortcut using this intent. Build and run the app to install it on the simulator. Then, open the Shortcuts app and create a new shortcut. Select the “Apps” category and choose BreakLogger. Add the “Log a Break” action to your shortcut. After saving the shortcut, you can run it by tapping on it. A dialog message will appear, confirming that the break has been logged. When you reopen the BreakLogger app, you’ll see the logged break in the list.
Next, you’ll add an app shortcut to BreakLogger. Create a new Swift file named “BreakLoggerShortcuts.swift” in the Intents group. In this file, create a struct called “BreakLoggerShortcuts” that conforms to the AppShortcutsProvider protocol. This struct will provide the app shortcuts to iOS. Implement the static property “appShortcuts” to return an array of AppShortcut instances. For this example, you’ll only provide one shortcut, which uses the LogBreakIntent and has two phrases that can trigger it. One phrase is “Log a break” and the other uses the special token “.applicationName” to dynamically insert the localized name of your app.
Build and run the app again. This time, you can use Siri on the simulator to trigger the shortcut by saying “Use BreakLogger to log a break”. Siri will prompt you to enable BreakLogger shortcuts and then confirm the action. When you open BreakLogger, you’ll see the break logged from the shortcut in the list.
In the next step, you’ll create a custom confirmation view to display whenever the intent is executed.
Source link