How to add Siri integration to iOS 10 apps
Reference By:TechRepublic
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.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
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.- Click the iOS Project in the Project Navigator.
- Click the + button to add a new Target or select File | New | Target.
- In the template chooser, select Intents Extension and then click Next.
- 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.
- Click Finish.
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).
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
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
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)
No comments: