Tuesday, September 18, 2018

iOS Siri Shortcuts - Basics

With shortcuts, users can access features of your app from places such as Spotlight search, Lock Screen, and the Siri watch face. With phrases, users can speak to Siri to access your app’s features.

Application can provide custom intent on a set of categories. This is done by adding an intent definition file to the project. A custom intent can define a set of parameters to accept and it can also define a type of response. Custom intent can also be marked for background execution. To use the details given in the custom intent definition file, app needs to define intent code.

To use custom intent in the app, Xcode need to generate the source code for the items defined inthe intent definition file. However, apps having the shared framework, the code generation should happen only for the shared framework, To specify which target has the generated source code, use the Intent Classes setting in the Target Membership panel. all other targets use the No Generated Classes setting.

Before Siri can suggest shortcuts to the user, the app must tell Siri about the shortcuts through intent donations.

private func donateInteraction(for order:Order) {
   let interaction = INInteraction(intent:order.intent, response:nil)
   interaction.identifier = order.identifier.uuidString
  
   interaction.donate { (error) in
       if (error == nil) {
        //no error. Interaction donation is successful. Great
    }
   }
}

The identifier can be used to cancel the intent if the user cancels the order.

Next task is to Handle the shortcut. Inorder to handle the shortcut, app provides an app extension, which handles the intent in background. When the app must be launched to handle the intent, the app is launched by the App Delegate call back below

override func restoreUserActivityState(_ activity : NSUSErActivity) {
    if(activity.activityType == NSUSerActivity.orderCompleteActivityType) {
       // this is order complete activity.
    }
}

All of the above was just about shortcut that appear in different places. How about if we could add a voice command to it? This can be done by adding custom voice phrases. This can be done by going to the app settings or even from the app following the below

Let addButton = INUIAddVoiceShortcutButton(style:.whiteoutline)
addShortcutButton.shortcut = INShortcut(intent:order.intent)
addShorcutButton.delegate = self





references:
https://developer.apple.com/documentation/sirikit/soup_chef_accelerating_app_interactions_with_shortcuts



No comments:

Post a Comment