Skip to content

Instantly share code, notes, and snippets.

@ajfigueroa
Last active August 21, 2018 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajfigueroa/84c4bf3bd0454705c4cca93cb767621f to your computer and use it in GitHub Desktop.
Save ajfigueroa/84c4bf3bd0454705c4cca93cb767621f to your computer and use it in GitHub Desktop.

WWDC 2018 - Introduction to Siri Shortcuts

  • Shortcuts let you expose the capabilities of your apps to Siri
  • Siri surfaces intents that people do with your apps in Search, Watch, and on Lock screen
    • Tapping on a shortcut can open it up inline with rich preview
  • It can also be used with Siri directly and can show a custom view screen
  • Apps can provide custom response dialog
    • It'll say things such as “how long it will take for your coffee to be ready”
  • Users can choose a custom phrase when adding a shortcut to Siri
    • Developers can suggest what phrase to use such as “Coffee time”
    • Once it's been added it'll work on all devices such as Watch and Homepod
  • Shortcuts app lets users build their own shortcuts
    • Including shortcuts defined by your app
  • 3 Steps for creating a Shortcut
    • Define a Shortcut
      • What you want to expose and each one so Siri knows what your app wants to do
    • Donate Shortcut
      • Tell the system everytime a user does something in the app that you have a shortcut for
    • Handle Shortcut
      • When user wants to use shortcut, you need to be ready for app or app extension to be invoked.
  • Consider what are the most important things people will want to do with your app
    • This is likely what you'll want to expose shortcuts for
  • Every shortcut should:
    • Accelerate the user to perform a key function of your app
    • Be of persistent interest to the user
    • Be executable at any time
  • There are two APIs that support Shortcut adoption
    • NSUserActivity
      • lightweight way to represent state of app
      • integrates with Spotlight and Hand Off
    • Intents
      • represent in detail type of action an app can perform
      • comes with built in intents that apps can use to integrate with Siri
      • custom intents can be defined
  • Choosing an Adoption strategy:
    • NSUserActivity
      • Opens something in your app
      • Represents showing items that you index in Spotlight or use for Handoff
    • Intents
      • Run inline without launching your app
      • Include custom voice response or a custom UI
      • Include granular predictions
  • Adopting with NSUserActivity
    • First define the shortcut!
    • Declare a type in NSUserActivityTypes in Info.plist
    • <key>NSUserActivityTypes</key>
      <array>
        <string>com.myapp.name.my-activity-type</string>
      </array>
      
    • We then need to donate the shortcut
    • Everytime a user is looking at a screen that corresponds with a Shortcut you want to expose in your app, you should create a NSUserActivity object
    • isEligibleForPrediction needs to be set to true to be a shortcut along with the isEligibleForSearch
    • Store all info you'll need to restore activity later on in userInfo
    • Mark activity as current by attaching it to UIViewController or UIResponder object that's on screen
      • viewController.userActivity = userActivity // marks as current
    • The shortcut should be handled via the AppDelegate method: continueUserActivity
      • If this activity type matches what was registered then we need to restore state of app
  • Adopting with Intents
    • Define the shortcut
    • Decide what type of intent you'd want to adopt?
      • Messaging, Workouts, Lists etc
      • Custom too!
    • Create an intent definition file in Xcode to customize a built-in SiriKit intent OR define a new custom intent
      • File → New File → SiriKit Intent Definition File
    • Example: Soup delivery app to allow easy way to order soup!
      • Intent Metadata
        • Give Intent a name: Order Soup
        • Category: Order
        • Description and Confirmation from User are additional metadata
      • Intent Parameters
        • Parameters are a dictionary
        • Custom types are allowed along with String, Number etc
      • Shortcut Types
        • All type of shortcuts you'd like to be suggested to the user
        • Along with formats of how to display parameters
        • Specify if Background use is supported
        • Background supported are suggested more frequently
      • Xcode will autogenerate Intent class and protocol for handling the Order
        • OrderSoupIntent: INIntent and OrderSoupIntentHandling protocol
        • Consider which target this is being added to
        • If you have no app specific frameworks, check the target for every single target in your app
      • To donate the intent object:
        • Insatiate the intent every time the user performs the event corresponding to the Shortcut
      • Handling custom intent
        • “continueUserActivity” is also used to handle the Intent
        • Passed as a NSUserActivity whose activityType is the name of the Intent
        • If you ONLY implement continueUserActivity then it'll open in foreground only
        • You'll want to create an extension: IntentExtensionTemplate in order to handle the Intent in the background
          • Conform to IntentHandling protocol in extension
          • In confirm, you should check that all properties are valid otherwise return the error code if you're unable to handle the intent
          • Then perform the shortcut with response object that indicates outcome
      • Implement intent extension for shortcuts that can run in background without launching the app
      • Implement -continueWithUserActivity even if you have an intents extensions
        • User can choose to open the app from the shortcut
    • INRelevantShortcut
      • Ways to expose shortcut to the Siri watchface
      • Works even if you don't have a watch app and they can be shown on Siri Watchface
  • How is a shortcut suggested?
    • The system will look at the time, location, and other signals
    • Time
      • Time of day and day of week
    • Location
      • Check if location is significant for that user (aka home, work etc)
  • Imagine a UserActivity for placing a soup order
    • Imagine UserInfo with Soup, Quantity, and scrollPosiiton
    • scrollPosition could change and Siri may be unable to find a pattern effectively
    • Specify requiredUserInfoKeys which is minimum info necessary required to define the intent
  • What makes a good donation?
    • Likely to be repeated
    • Ensure payload donating is consistent
    • Don't include timestamps
      • Example reminder on June 5th vs today
    • Donate once and only for each user action
  • Custom Intents
    • Use enums when the values for a parameter are clearly bounded
      • Ex. Size for soup would be good candidate
    • Custom results in INObject
      • INObject combines identifier with a display string
      • Identifier references internal object
      • display string conveys object to the user
      • Prevents possible implicit dependencies between parameters
  • Shortcut Display
    • Developer settings let you see donated shortcuts in Spotlight
    • Editing Xcode schemes to automatically invoke Siri with Phrase
      • Siri Intent Query field with phrase to activate Siri
    • Create a Custom Shortcut using Shortcuts app
  • Privacy Considerations
    • Users expect that when you delete something from app that it's deleted for good
    • Don't suggest content that's no longer relevant
      • Delete donations when something gets deleted
      • When user logs out, you can delete all saved user activities
    • Delete invalid donations when appropriate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment