天天看点

Siri Integration in iOS 10 with Swift – SiriKit Tutorial (Part 1)

This tutorial written on June 13th, 2016 using the Xcode 8 Beta 1, and is using the Swift 3.0 toolchain.

Get Xcode 8 set up for iOS 10 and Swift 3 compilation.

If you have not yet downloaded Xcode 8 Beta 1, please do so here . 

(Optional) Compiling from the command line

To opt in to the Swift 3.0 toolchain you shouldn’t need to change anything unless you want to build from the command line. If you plan to build from the command line, open Xcode-beta and from the OS menu bar select Xcode > Preferences . Then select the Locations tab. At the bottom of the page here you will see “Command Line Tools”. Make sure this is set to Xcode 8.0. 

Now if you navigate to the project directory containing the .xcodeproj file, you can optional compile your project by calling 

xcodebuild

 from the command line. 

(Optional) Migrating from an existing Swift 2 app

If you are working with an existing Swift 2 project and want to add Siri integration with Swift 3.0, click on the root of your project and select Build Settings . Under Swift Compiler – Version , find the field labeled Use Legacy Swift Language Version and set it to No . This will lead to compiler errors most likely that you will need to fix throughout your project, but it’s a step I recommend to keep up with Swift’s ever-changing semantics. 

Getting started with SiriKit

First, in your app (or in a new single-view Swift app template if you are starting fresh), switch to the general view by selecting the root of your project. Under this tab you can click the (+) icon in the lower land corner of the side-pane on the left. From the dropdown that appears selection iOS > Application Extension , and then select Intents Extension . 

Siri Integration in iOS 10 with Swift – SiriKit Tutorial (Part 1)

This adds a new intent to the project, and we’ll use it to listen for Siri commands. The product name should be something similar to your app so it’s easy to identify, for example if your app is called MusicMatcher , you could call the Product Name of this intent MusicMatcherSiriIntent . Make sure to also check the checkbox to Include UI Extension . We will need this later in the tutorial, and it’s easiest to just include the additional extension now. 

Siri Integration in iOS 10 with Swift – SiriKit Tutorial (Part 1)

What we’ve created are two new targets as you can see in the project heirarchy. Let’s jump in to the boilerplate code and take a look at the example in the IntentHandler.swift file inside of the Intent extension folder. By default this will be populated with some sample code for the workout intent, allowing a user to say commands such as “Start my workout using MusicMatcher”, where MusicMatcheris the name of our app. 

Siri Integration in iOS 10 with Swift – SiriKit Tutorial (Part 1)

Run the Template App as-is

It’s helpful at this point to compile this code as-is and try out the command on an actual iOS device. So go ahead and build the app target, by selecting the app MusicMatcher from the Scheme dropdown, and when the target device set to your test iOS device, press the Build & Run button. 

Siri Integration in iOS 10 with Swift – SiriKit Tutorial (Part 1)

You should see a blank app appear, and in the background your extensions will also be loaded in to the device’s file system. Now you can close your app using the Stop button in Xcode to kill the app.

Then, switch your scheme to select the Intent target, and press build & run again.

Siri Integration in iOS 10 with Swift – SiriKit Tutorial (Part 1)

This will now prompt asking which app to attach to, just select the app you just ran, MusicMatcher . This will present the app again on your device (a white screen/blank app most likely), but this time the debugger will be attached to the Intent extension. 

Siri Integration in iOS 10 with Swift – SiriKit Tutorial (Part 1)

You can now exit to the home screen by pressing the home button, or the app may exit on it’s own since you are running the Intent and not the app itself (This is not a crash!!!)

Enable the extension

The extension should now be in place, but we as an iOS device user still may need to enable the extension in our Siri settings. On your test device enter the Settingsapp. Select the Siri menu, and near the bottom you should see MusicMatcher listed as a Siri App. Make sure the app is enabled in order to enable Siri to pick up the intents from the sample app. 

Testing our first Siri command!

Try the Siri command. Activate Siri either by long pressing the Home button, or by saying “Hey Siri!” (note the “Hey Siri!” feature must be enabled in the settings first)

Try out some of the command “Start my workout using MusicMatcher”.

“Sorry, you’ll need to continue in the app.”

If you’re like me this will bail with an error saying “Sorry, you’ll need to continue in the app.” (For some reason this occassionally was not a problem. Ghosts?)

In the console you may see something like this:

dyld:

Library

not loaded:

@rpath

/libswiftCoreLocation.dylib

Referenced

from: /

private

/

var

/containers/

Bundle

/

Application

/

CC815FA3

-

EB04

-4322-

B2BB

-8E3F960681A0/

LockScreenWidgets

.app/

PlugIns

/

JQIntentWithUI

.appex/

JQIntentWithUI

Reason

: image not found

Program

ended with exit code: 1

We need to add the CoreLocation library to our main project, to make sure it gets copied in with our compiled Swift code.

Select the project root again and then select your main MusicMatcher target. Here under General you’ll find area area for Linked Frameworks and Libraries . Click the (+) symbol and add CoreLocation.framework. Now you can rebuild and run your app on the device, then follow the same steps as above and rebuild and run your intent target. 

Finally, you can activate Siri again from your home screen.

“Hey Siri!”“Start my workout using MusicMatcher” 

Siri should finally respond, “OK. exercise started on MusicMatcher” and a UI will appear saying “Workout Started”

Siri Integration in iOS 10 with Swift – SiriKit Tutorial (Part 1)

How does it work?

The IntentHandler class defined in the template uses a laundry list of protocols:

First an foremost is 

INExtension

 which is what allows us to use the class as an intent extension in the first place. The remaining protocols are all intent handler types that we want to get callbacks for in our class: 

INStartWorkoutIntentHandling

INPauseWorkoutIntentHandling

INResumeWorkoutIntentHandling

INCancelWorkoutIntentHandling

INEndWorkoutIntentHandling

The first one is the one we just tested, 

INStartWorkoutIntentHandling

 . 

If you command-click this protocol name you’ll see in the Apple docs this documentation:

继续阅读