Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to Siri Shortcuts

Mathis
August 30, 2018
110

Intro to Siri Shortcuts

Mathis

August 30, 2018
Tweet

Transcript

  1. Adopting Shortcuts • Easy to adopt • Just a shortcut

    to your app • Already used for Handoff & Spotlight • Can run in background • Can show custom UI and custom responses • Allows for better predictions
  2. NSUserActivity 1. Define Shortcut: Declare a user activity type <key>NSUserActivityTypes</key>

    <array> <string>com.myapp.name.my-activity-type</string> </array>
  3. NSUserActivity 2. Donate Shortcut: Create a NSUserActivity object let userActivity

    = (activityType: "com.myapp.name.my-activity-type") userActivity.isEligibleForSearch = true userActivity.isEligibleForPrediction = true userActivity.title = "Activity" userActivity.userInfo = ["key": "value"] userActivity.suggestedInvocationPhrase = "Let's do it” let attributes = CSSearchableItemAttributeSet(itemContentType: kUTTypeItem as String) let image = UIImage(named: "myImage")! attributes.thumbnailData = image.pngData() attributes.contentDescription = "Subtitle" userActivity.contentAttributeSet = attributes viewController.userActivity = userActivity
  4. NSUserActivity 3. Handle Shortcut: Implement the App Delegate method func

    application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool { if userActivity.activityType == "com.myapp.name.my-activity-type" { } // Restore state for userActivity and userInfo }
  5. Intents • Can use custom intents or rely on built-in

    intent-domains (SiriKit) • Should use built-in domains if possible
  6. Intents • Xcode will generate an Intent and an IntentHandling

    Protocol public class ViewMagazineIntent: INIntent { public var magazine: INObject? } public protocol ViewMagazineIntentHandling: NSObjectProtocol { public func handle(intent: ViewMagazineIntent, completion: @escaping (ViewMagazineIntentResponse) -> Void) optional func confirm(intent: ViewMagazineIntent, completion: @escaping (ViewMagazineIntentResponse) -> Void) }
  7. Intents 2. Donate the shortcut let viewMagazineIntent = ViewMagazineIntent() viewMagazineIntent.magazine

    = INObject(identifier: magazine.identifier, display: magazine.title) viewMagazineIntent.suggestedInvocationPhrase = "Show Magazine!" let interaction = INInteraction(intent: viewMagazineIntent, response: nil) interaction.donate { error in guard error == nil else { // Handle errors } }
  8. Intents 3. Handle the shortcut (in the app delegate) func

    application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool { if userActivity.activityType == "ShowMagazineIntent", let intent = userActivity.interaction?.intent as? ViewMagazineIntent { // Present magazine, pre-populated with the fields from the intent } }
  9. Intents 3. Handle the shortcut (in the background in an

    extension) public class ViewMagazineIntentHandler: NSObject, ViewMagazineIntentHandling { public func confirm(intent: ViewMagazineIntent, completion: @escaping (ViewMagazineIntentResponse) -> Void) { completion(ViewMagazineIntentResponse(code: .ready, userActivity: nil)) } public func handle(intent: ViewMagazineIntent, completion: @escaping (ViewMagazineIntentResponse) -> Void) { guard let magazine = intent.magazine else { completion(ViewMagazineIntentResponse(code: .failure, userActivity: nil)) return } print("Handled magazine with identifier = \(String(describing: magazine.identifier))") completion(ViewMagazineIntentResponse(code: .success, userActivity: nil)) } }
  10. (Lots) More • Better suggestion: requiredUserInfoKeys / parameter combinations for

    intents • Custom results with INObject • Custom UI responses • INPlayMediaIntent for Media. Works on HomePod. • Expose Shortcuts on Siri Watch Face via INRelevantShortcuts • Use INVoiceShortcutCenter to suggest initial shortcuts • INUIAddVoiceShortcutViewController to let the user set a shortcut
  11. Tips & Good Practices • Donate shortcuts only once •

    Privacy: Delete shortcuts when they are no longer needed • Testing: • User the new developer options • Create a custom shortcut in the Shortcuts app
  12. Sessions and Resources • Session 211: Introduction to Siri Shortcuts

    • Session 214: Building for Voice with Siri Shortcuts • Sample Code: Soup Chef • SiriKit Documentation • SiriKit Landing Page • HIG on SiriKit