SiriKit Brief Explanation


What is SiriKit ?

SiriKit is sort of framework developed by Apple, used to make your IOS application to be compatible with Siri. By implementing it, you can process your application by using the Siri command. Till the date of writing, Siri support all of these application domains:

  • VoIP calling : Initiate call and search the user's call history
  • Messaging : Send messages and search the user's receive messages
  • Payments : Send payments between user or pay bills
  • Lists and Notes : Create and manage notes and to-do list items
  • Visual Codes : Convey contact and payment information using Quick Response (QR) Code
  • Photos : Search and display photos
  • Workouts : Start, end and manage fitness routines
  • Ride Booking : Book rides and report their status
  • Car Command : Manage vehicle door locks and get the vehicle's status
  • CarPlay : Interact with the vehicle's CarPlay system
  • Restaurant Reservation : Create and manage restaurant reservation with help from the Map app.

Basic Knowledge of SiriKit

Although, It's named as SiriKit but there is no framework call SiriKit. It consist of following two framework:

  • Intent Framework
  • Intent UI Framework

The Intent Framework provides basic function for the device and application to interact with each other. On the other hand, Intent UI Framework provides a function to display the result of processing executed via Siri on the screen.

Sample Application

We will walk through and create one sample project for this. We will ask the Siri to open our Application by giving our custom command.

Now, let's go and create a New project on your Xcode and then go ahead and enable Siri Capability in your project:

We can run this application in either the simulator or the real device.
In case, you are using simulator, you can open up Siri by simply press (command + alt + shift + H)

So next, we will add the Privacy - Siri Usage Description to our info.plist file.

Next, we have to request for Siri Authorization. So open up the ViewController.swift file and import Intents into it.

import Intents

and then insert this block of code to request for Siri Authorization:

INPreferences.requestSiriAuthorization { (status) in }

SiriKit give us the capability to expand the Siri vocabulary. Add this following code to give Siri the capability of understanding our own command.

INVocabulary.shared().setVocabularyStrings(["Open", "SiriKit", "Sample"], of: .workoutActivityName)

In my case, I added three word for my custom command.
And next, we have to enable Siri support by building Siri extension. Now in Xcode go ahead and click File -> New -> Target -> Intent Extension and give the name as Intent Handler. After that you can see the file name IntentHandler.swift under Intent Handler directory. Go ahead open it and clear some code from that file and keep it as shown below:

import Intents

class IntentHandler: INExtension {

}

Now, we will start by implement two protocols in IntentHandler class which are INStartWorkoutIntentHandling and INEndWorkoutIntentHandling. Now let add its protocol stub to the class and the code should look like this till now:

class IntentHandler: INExtension, INEndWorkoutIntentHandling, INStartWorkoutIntentHandling {

    func handle(startWorkout intent: INStartWorkoutIntent, completion: @escaping (INStartWorkoutIntentResponse) -> Void) {

    }

    func handle(endWorkout intent: INEndWorkoutIntent, completion:       @escaping (INEndWorkoutIntentResponse) -> Void) {

    }

}

Now let's go ahead and add this code to the function

func handle(startWorkout intent: INStartWorkoutIntent, completion: @escaping (INStartWorkoutIntentResponse) -> Void) {

    print("Start Workout Intent:", intent)

    let userActivity: NSUserActivity? = nil
    guard let spokenPhrase = intent.workoutName?.spokenPhrase else {
        completion(INStartWorkoutIntentResponse(code: .failureNoMatchingWorkout, userActivity: userActivity))
        return
    }

    print(spokenPhrase)

    completion(INStartWorkoutIntentResponse(code: .continueInApp, userActivity: userActivity))
}

So now let's implement handling app launch from Siri:
In your AppDelegate.swift

import Intents

and add this func into it:

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
        guard let intent = userActivity.interaction?.intent as?INStartWorkoutIntent else {
            print("AppDelegate: Start Workout Intent - FALSE")
            return false
        }
        print("AppDelegate: Start Workout Intent - TRUE")
        print(intent)
        return true
}

Now you can start running your application and see the result. when running your application, an alert dialog will be prompted from your Xcode to choose an app to run. So just choose Siri and you are ready to go:

I also have a GitHub link for the above article, So that you can go and check the full sample code and grape the basic understanding of this SiriKit.
Here is the link : https://github.com/phanithken/SiriKitSample