- Published on
Getting Started with SiriKit in iOS
- Authors
- Name
- Brahim El mssilha
- @siempay
Introduction
SiriKit allows iOS apps to interact with Siri, letting users perform tasks in your app using voice commands. By integrating SiriKit into your app, you can make it more accessible and interactive. In this tutorial, we’ll cover the basics of SiriKit and show you how to integrate it into your app.
Table of Contents
- What is SiriKit?
- Setting Up SiriKit
- Adding Siri Support to Your App
- Testing SiriKit Integration
- Best Practices
- References
What is SiriKit?
SiriKit allows apps to expose their functionalities to Siri and Shortcuts. It provides a set of Intents, which represent specific actions that Siri can handle, such as sending messages, starting workouts, or booking rides. By defining these intents, Siri can interact with your app through voice commands.
Supported Domains
SiriKit works with predefined domains, which define the types of tasks users can perform with your app. Some examples of supported domains are:
- Messaging
- Payments
- Ride Booking
- Workouts
- VoIP Calling
- Car Commands
- Lists and Notes
Apps outside of these domains can still use Siri Shortcuts for custom intents.
Setting Up SiriKit
Before Siri can interact with your app, you need to enable SiriKit in your Xcode project and configure the necessary intents.
Adding Siri Support to Your App
Step 1: Enable Siri in Xcode
- Open your Xcode project.
- Go to Signing & Capabilities and click the + Capability button.
- Search for Siri and add it to your app.
This adds the necessary entitlements to your app to support Siri interactions.
Step 2: Define Intents
Intents define what your app can do via Siri. You can either use predefined intents for domains like messaging and workouts, or create custom intents.
For example, if your app provides a workout feature, you can use the predefined INStartWorkoutIntent
.
Create an Intents Extension: Siri interactions are handled in a separate extension, so create one by going to File > New > Target and choosing Intents Extension.
Declare Supported Intents: In your
Info.plist
for the intents extension, declare the supported intents:
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>IntentsSupported</key>
<array>
<string>INStartWorkoutIntent</string>
</array>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.intents-service</string>
</dict>
Step 3: Implement Intent Handling
Next, you need to implement the logic to handle the intent. This is done in the IntentHandler
class inside the Intent Extension.
For example, if you're handling a INStartWorkoutIntent
, the class might look like this:
import Intents
class IntentHandler: INExtension, INStartWorkoutIntentHandling {
// Resolve the workout type (optional)
func resolveWorkoutName(for intent: INStartWorkoutIntent, with completion: @escaping (INSpeakableStringResolutionResult) -> Void) {
if let workoutName = intent.workoutName {
completion(.success(with: workoutName))
} else {
completion(.needsValue())
}
}
// Confirm the intent before executing
func confirm(intent: INStartWorkoutIntent, completion: @escaping (INStartWorkoutIntentResponse) -> Void) {
// Perform any checks before starting the workout
completion(INStartWorkoutIntentResponse(code: .ready, userActivity: nil))
}
// Handle the intent to start the workout
func handle(intent: INStartWorkoutIntent, completion: @escaping (INStartWorkoutIntentResponse) -> Void) {
// Logic to start the workout
// ...
completion(INStartWorkoutIntentResponse.success(workoutName: intent.workoutName!))
}
}
Step 4: Provide Siri Suggestions
You can provide Siri with relevant suggestions based on the user’s behavior, making Siri more useful and personalized. This can be done using NSUserActivity or INInteraction.
Using NSUserActivity:
let activity = NSUserActivity(activityType: "com.example.myApp.startWorkout")
activity.title = "Start a Workout"
activity.userInfo = ["workoutType": "Run"]
activity.isEligibleForSearch = true
activity.isEligibleForPrediction = true
activity.persistentIdentifier = NSUserActivityPersistentIdentifier("com.example.myApp.startWorkout")
view.userActivity = activity
view.userActivity?.becomeCurrent()
Using INInteraction (for more complex interactions):
let intent = INStartWorkoutIntent(workoutName: "Run")
let interaction = INInteraction(intent: intent, response: nil)
interaction.donate { (error) in
if let error = error {
print("Interaction donation failed: \(error)")
} else {
print("Successfully donated interaction")
}
}
Testing SiriKit Integration
To test SiriKit in your app:
- Run the app on a device: Siri interactions do not work on the iOS simulator, so you’ll need to run the app on an actual iOS device.
- Activate Siri: Invoke Siri using "Hey Siri" or by pressing the side button, and test your intents.
- Debug with Logs: Use print statements and breakpoints in your intent handler to track the flow of intent resolution and handling.
Best Practices
- Keep the Voice User Interface in Mind: When designing Siri interactions, ensure that the responses are short and meaningful, as they will be presented through voice.
- Handle Errors Gracefully: Always provide fallback options in case something goes wrong while processing the intent (e.g., missing data or invalid parameters).
- Test for Accessibility: Ensure that Siri interactions make sense and are useful for users relying on voice commands.
- Update Intent Responses Regularly: As your app evolves, ensure the supported intents and their responses are kept up to date with any new functionality.
References
By integrating SiriKit into your app, you can make your app more engaging and accessible through voice commands. Whether you're working with predefined intents or creating custom ones, SiriKit offers a way to enhance user interaction with your app.