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:

- <b>VoIP calling</b> : Initiate call and search the user's call history
- <b>Messaging</b> : Send messages and search the user's receive messages
- <b>Payments</b> : Send payments between user or pay bills
- <b>Lists and Notes</b> : Create and manage notes and to-do list items
- <b>Visual Codes</b> : Convey contact and payment information using Quick Response (QR) Code
- <b>Photos</b> : Search and display photos
- <b>Workouts</b> : Start, end and manage fitness routines
- <b>Ride Booking</b> : Book rides and report their status
- <b>Car Command</b> : Manage vehicle door locks and get the vehicle's status
- <b>CarPlay</b> : Interact with the vehicle's CarPlay system
- <b>Restaurant Reservation</b> : 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 <i>Intent Framework</I> 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:

<img width="1547" alt="screenshot.png" src="https://qiita-image-store.s3.amazonaws.com/0/211892/1022d770-5165-c847-f395-c7e14dbf0a52.png">

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 <i>Privacy - Siri Usage Description</i> to our info.plist file.
<img width="890" alt="screenshot.png" src="https://qiita-image-store.s3.amazonaws.com/0/211892/94be8037-58df-b257-73b7-b72f359643c7.png">

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

```swift
import Intents
```
and then insert this block of code to request for Siri Authorization:

```swift
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.

```swift
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 <b>File -> New -> Target -> Intent Extension</b> and give the name as <b>Intent Handler</b>. After that you can see the file name <i>IntentHandler.swift</i> under Intent Handler directory. Go ahead open it and clear some code from that file and keep it as shown below:

```swift
import Intents

class IntentHandler: INExtension {

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

```swift
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

```swift
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 <i>handling app launch from Siri</I>:
In your <b>AppDelegate.swift</b>

```swift
import Intents
```
and add this func into it:

```swift
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:
<img width="1142" alt="screenshot.png" src="https://qiita-image-store.s3.amazonaws.com/0/211892/afd764a1-c16f-8d1a-9448-4143040f2404.png">

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 : <a href="https://github.com/phanithken/SiriKitSample">https://github.com/phanithken/SiriKitSample</a>