$30 off During Our Annual Pro Sale. View Details »

Intro to Siri Shortcuts

Mathis
August 30, 2018
89

Intro to Siri Shortcuts

Mathis

August 30, 2018
Tweet

Transcript

  1. Intro to Siri Shortcuts
    Mathis Müller

    View Slide

  2. Overview
    • Rationale

    • Adopting shortcuts

    • User Activity

    • Intents

    • Demo

    • More

    View Slide

  3. View Slide

  4. View Slide

  5. View Slide

  6. Adopting Shortcuts
    Define Shortcut Donate Shortcut Handle Shortcut

    View Slide

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

    View Slide

  8. NSUserActivity

    View Slide

  9. NSUserActivity
    1. Define Shortcut: Declare a user activity type
    NSUserActivityTypes

    com.myapp.name.my-activity-type

    View Slide

  10. 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

    View Slide

  11. 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
    }

    View Slide

  12. Intents

    View Slide

  13. Intents
    • Can use custom intents or rely on built-in intent-domains (SiriKit)

    • Should use built-in domains if possible

    View Slide

  14. Intents
    1. Define the shortcut

    • Create an intent-definition file

    View Slide

  15. View Slide

  16. 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)
    }

    View Slide

  17. 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
    }
    }

    View Slide

  18. 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
    }
    }

    View Slide

  19. Intents
    3. Handle the shortcut (in the background in an extension)

    View Slide

  20. 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))
    }
    }

    View Slide

  21. (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

    View Slide

  22. 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

    View Slide

  23. 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

    View Slide