Integrating Siri on your iOS app

Julián Astrada
4 min readOct 30, 2023

In the ever-evolving landscape of iOS app development, staying ahead of the curve is essential. One way to take your iOS app to the next level is by integrating Siri, Apple’s voice-controlled virtual assistant. By incorporating Siri into your app, you can offer users a more interactive and convenient experience. In this blog post, we will guide you through the process of integrating Siri into your iOS application, making it more user-friendly and engaging.

Why Integrate Siri into Your iOS App?

  1. Enhanced User Experience: Siri adds a new layer of convenience and interactivity, allowing users to perform tasks in your app using voice commands. This streamlines user interactions and makes the app more accessible.
  2. Improved Engagement: Voice-activated functionalities can increase user engagement by offering a hands-free, natural way to interact with your app. This can lead to longer user sessions and higher retention rates.
  3. Accessibility: Siri makes your app accessible to a wider audience, including people with disabilities and those in situations where touch-based interaction is impractical, such as while driving.
  4. Competitive Advantage: Siri integration sets your app apart from the competition, signaling your commitment to staying on the cutting edge of technology and user experience.

Adding a NSUserActivity:

Create a New NSUserActivity:

  • In your code, create an instance of NSUserActivity and configure it to represent a specific user activity in your app. For instance, if you have a to-do list app, you might create an NSUserActivityfor creating a new task.
let userActivity = NSUserActivity(activityType: "com.yourapp.createTask")

Set User Activity Properties:

  • Customize your NSUserActivity to include details about the activity, like the title, userInfo, and required domain identifiers.
userActivity.title = "Create a New Task" 
userActivity.userInfo = ["taskTitle": ""]
userActivity.requiredUserInfoKeys = Set(["taskTitle"])
userActivity.isEligibleForSearch = true
userActivity.isEligibleForPrediction = true
userActivity.persistentIdentifier = NSUserActivityPersistentIdentifier("com.yourapp.createTask")

Donate the User Activity:

  • To make Siri aware of this user activity, you need to donate it to the system. This is the process of suggesting to Siri that your app can perform this specific action.
userActivity.becomeCurrent()

Donating a Suggestion to Siri:

Import Intents:

  • Make sure you import the Intents framework into your project, which allows you to work with Siri and donations.
import Intents

Create a Custom Intent:

  • If you haven’t already, create a custom intent that represents the action you want to donate to Siri. This is essential for Siri to understand the user’s request.

Donate the Suggestion:

  • In your code, donate the user activity by creating an INInteraction object and donating it to the system. This indicates that your app can perform the action associated with the user activity.
if let interaction = INInteraction(intent: yourCustomIntent, response: nil) { 
interaction.donate { (error) in
if let error = error {
print("Donation error: \(error)")
} else {
print("Donation successful!")
}
}

Testing:

By adding NSUserActivity and donating suggestions to Siri, you make your app's functionality accessible through voice commands, improving the overall user experience. It's essential to keep these activities and suggestions up-to-date, especially when adding new features to your app, to ensure Siri always understands the capabilities of your application.

Creating Siri Shortcuts from a Button in Your iOS App

Now, let’s explore how to create a Siri shortcut from a button within your iOS app. This feature allows users to trigger specific actions using voice commands through Siri.

Create a Button:

  • In your app’s user interface, create a button that represents the action you want users to perform via Siri.

Add an Action:

  • Implement a method that is triggered when the button is tapped. This method should include code to perform the action associated with the button.
@IBAction func performAction(_ sender: UIButton) { 
// Your action code here
}

Create an INUIAddVoiceShortcutViewController:

  • Implement a view controller that allows users to add a voice shortcut to the action associated with the button.
let shortcutViewController = INUIAddVoiceShortcutViewController(shortcut: yourCustomIntent) 
shortcutViewController.delegate = self

present(shortcutViewController, animated: true, completion: nil)

Handle the Result:

  • Implement the delegate methods to handle the result when the user adds a voice shortcut.
func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith voiceShortcut: INVoiceShortcut?, error: Error?) { 
// Handle the result here
dismiss(animated: true, completion: nil)
}

By following these steps, you can allow users to create Siri shortcuts for specific actions within your app. This makes it even more user-friendly and convenient for users who prefer voice commands.

Conclusion

Integrating Siri into your iOS app and creating Siri shortcuts can work wonders for your user experience, engagement levels, and your app’s competitive edge. By following the steps outlined in this guide and keeping things user-friendly and fun, you can make your app accessible to a broader audience and stay ahead of the game in the ever-evolving world of mobile app development. Siri integration and shortcuts are like the magic wands that can make your app truly user-centric and innovative. So, go ahead and add that touch of Siri magic to your iOS app!

Originally published at https://julianastrada.com on October 30, 2023.

--

--