Developer

How to add Siri integration to iOS 10 apps

In iOS 10, developers can use SiriKit to add voice interaction to their apps. Follow this walk-through on adding Siri integration to iOS 10 apps.

One of the final frontiers for mobile app extensions was the ability to interact with Siri—many developers have been wanting this functionality for years. Now that's possible with SiriKit.

SiriKit, which was released at WWDC 2016, is a way for developers to add voice interaction through Siri into their iOS 10 apps. This is done through the use of Intents and the app extension paradigm that was released inside iOS 8; this process provides a safe way for apps to touch system features and offers user features outside of the app.

Here's an overview of what Intents are and how they can be used. Then, we'll create a sample app that shows how to display a UI inside of Siri from your extension.

SEE: Apple's Siri: The smart person's guide (TechRepublic)

What are Intents?

Intents are the way that your app processes user interaction and provides information back to Siri and the user.

There are two components to Intents: the Intents extension and the Intents UI extension. The Intents extension is a requirement, and it's how your app will receive the information from the Siri request in order to process it. The Intents UI extension is an optional component that allows you to provide a visible interface inside of Siri once the Intent is handled successfully by your Intents extension.

sirikithero.jpg

With Intents, the domains (or types of apps) that can be used with Siri include:

  • VoIP calling
  • Messaging
  • Payments
  • Photos
  • Workouts
  • Ride Booking
  • Car Commands
  • CarPlay
  • Restaurant Reservations

There is hope that more domains will be added to Siri in order to provide a wide gamut of flexibility for Siri-enabled apps. For now, your app must fall into one of these categories in order to implement Siri.

SEE: How to use third-party apps with Siri (TechRepublic)

How to create the sample app

Let's create a sample app. If you don't already have an Xcode project, create an iOS project file using the Single View template, and then follow these steps to create the Intents extension.

  1. Click the iOS Project in the Project Navigator.
  2. Click the + button to add a new Target or select File | New | Target.
  3. In the template chooser, select Intents Extension and then click Next.
  4. In this new panel (Figure A), we'll name the extension MyIntent. Also, if needed, ensure that the Include UI Extension checkbox is checked; this will automatically create the Intent UI extension alongside the Intent extension.
  5. Click Finish.

Figure A

sirikitfigurea.jpg

When adding a target, ensure that the proper iOS app is selected for the Embed In Application option.

Once the targets are created, you'll see two new folders in the project navigator: MyIntent and MyIntentUI. Both folders have an Info.plist and a swift file for the source; however, the Intent UI extension has a storyboard and view controller file (Figure B).

sirikitfigureb.jpg

MyIntent is the Intent extension, and MyIntentUI is the optional UI extension that provides the interface to Siri.

The final thing to configure in the project is to ensure that Siri is enabled in the Capabilities tab. Select the project file in the navigator, select the iOS target, and then select the Capabilities tab. Turn the switch for Siri to the ON position.

How to register Intents

Registering an Intent is a two-part process. By registering an Intent, you are essentially telling Siri which actions your app can perform through Siri. Visit the Apple Developer page for SiriKit Intents to find the names of the Intents that can be registered.

The first part of this registration process is to register the supported Intent inside of Info.plist in the Intent target (Figure C).

Figure C

sirikitfigurec.jpg


We'll use the Ride Booking Intent for this sample, so edit the Info.plist for MyIntent to look like the one above, ensuring that it contains the following keys:

  • INRequestRideIntentHandling
  • INRequestRideIntent
  • INRequestRideIntentResponse

The second part of registering Intents is to open IntentHandler.swift inside of the MyIntent class. Inside of this class, you'll notice that it conforms to different protocols based on the Intents that were registered in the Info.plist.

Edit the IntentHandler.swift file to look like the following:

import Intents

class IntentHandler: INExtension, INRequestRideIntentHandling {
override func handler(for intent: INIntent) -> Any {
// This is the default implementation.  If you want different objects to handle different Intents,you can override this and return the handler you want for that particular intent.
return self
}
func handle(requestRide intent: INRequestRideIntent, completion: @escaping (INRequestRideIntentResponse) -> Void) {
let status = INRideStatus()
status.rideIdentifier = "12345"
let response = INRequestRideIntentResponse.init(code: .failure, userActivity: nil)
response.rideStatus = status
completion(response)
}
}

In the above example handler, whenever the user requests to book a ride in Siri with the app, it will respond with a failure status since we don't actually have a booking service. This code is merely to demonstrate how this process works and not to build an actual working service.

If you want to include more functionality, you can implement additional Intent types. Refer to the Apple Developer documentation for the required protocol conformity for the various Siri-capable Intents.

In this sample project, we will not utilize the Intents UI to provide an interface to the user.

SEE: How we learned to talk to computers, and how they learned to answer back (TechRepublic)

How to authorize the app to work with Siri

Once the handler code is written, all that's left is to add the code to the iOS app that allows for authorization. The first step is to open the Info.plist file for the iOS app and add the Key "NSSiriUsageDescription" with a user-facing String value that explains how Siri is used in the app and why the app needs authorization from the user. For this sample app, we'll go with "Uses Siri to book a ride."

Next, somewhere in the iOS app code, you'll need to request authorization from iOS and the user to use Siri. For this sample app, we'll do this in AppDelegate.swift in the application:didFinishLaunchingWithOptions: method, by adding the following code:

INPreferences.requestSiriAuthorization { (status: INSiriAuthorizationStatus) in
// Handle Siri authorizations status here
}

This block will be called back when authorization completes and will allow you to check the status to see if the user authorized the app to work with Siri. When calling this from your iOS code, don't forget to import the Intents framework.

It's time to run the app and see if we can get a response from Siri (we were looking for a failure response when requesting to book a ride). Build and run the app on a device, and then open the app and grant authorization with Siri. Next, open Siri and say "Book a ride with [App Name]" where App Name is the name of that app you created. When you do this and get an error, you'll notice that Siri offers another ride-booking app.

SEE: Siri's legacy: How next-gen bots will change the way business gets done (TechRepublic)

Give SiriKit a try

I hope you have a better understanding of how to use SiriKit inside your iOS 10 apps to extend the user experience outside of your apps. Please share your thoughts and tips on using SiriKit in the article discussion.

Also see

About Cory Bohon

Cory Bohon is an indie developer specializing in iOS and OS X development. He runs a software company called Cocoa App and is also a developer at MartianCraft. He was introduced to technology at an early age and has been writing about his favorite te...

Editor's Picks

Free Newsletters, In your Inbox