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

Make our Siri better by Mohit Anand

Make our Siri better by Mohit Anand

Swift Bangalore Chapter #18


Swift India

October 19, 2019


  1. Make Our Siri Better Siri Shortcuts 18th October 2019 By

    Mohit Anand
  2. TimeLine Pre iOS12 Limited Domains with Limited Intents
 (Making payments,

    working with notes, helping with your workouts )
  3. iOS 12 Siri Shortcuts
 Create custom intent to represent any

 Limitation: No multi step actions
  4. iOS 13 & above Conversational Siri Shortcuts
 Create custom intents

    with multi step actions. Siri can now ask follow-up questions.
  5. Creating Siri Shortcuts • Using NSUserActivity • Using Custom Intent

  6. Demo App We will add this in-app top up feature

    to Siri knowledge system Top up Feature • List of Funds • Amount to top up for a fund • Success screen
  7. Using NSUserActivity

  8. • Create a NSUserActivity first • Creating a shortcut with

    NSUserActivity is very simple.
  9. • Add your shortcut to Siri knowledge system by donating

    a NSUserActivity object every time user performs an action for which you want to create a shortcut. • Make your NSUserActivity, eligible for Siri Shortcut by a simple flag introduced in iOS 12.0. This will tell Siri that it can suggest this user activity as a shortcut to user.
  10. • Once your NSUserActivity is donated to system, it will

    be visible in Gallery section of Siri shortcuts app.
  11. • We can also Suggest a phrase which you want

    user to use for your shortcut by instance property introduced in iOS 12.0
  12. None
  13. • Handle NSUserActivity in your App Delegate This will make

    sure, user activity directly goes to our topup page where user can select a fund to top up.
  14. Let’s see, what we have built?

  15. Top up Shortcut via NSUserActivity • User says the selected

    phrase • Siri identifies it, and opens the appropriate application with the the same NSUSerActivity attached to the intent.
  16. But, we can do it better.

  17. Using Custom Intent

  18. • With custom intents, we can execute our feature directly

    in Siri, without opening the app. • For creating custom intent, we need to create an Intent Definition file • Intent Definition file is where all custom intents are defined.
  19. Create Intent Definition file

  20. Intent Definition File

  21. Category makes it easier for siri to talk about the

  22. None
  23. Choose the best category that describes your intent. For our

    use case, we will set our category as ORDER
  24. By Selecting the confirmation checkbox, siri ask user for the

    confirmation before completing the action. It is by default checked for some destructive or payment categories.
  25. None
  26. Parameters are inputs you want from user to complete the

  27. Parameters for Topup • Fund - Which fund to top

    up? • Amount - What amount user want to top up?
  28. None
  29. Parameter on top will be asked first by Siri

  30. None
  31. • For each parameter, we need to define required fields

  32. None
  33. None
  34. None
  35. None
  36. None
  37. None
  38. Done with the configuration

  39. Xcode automatically creates intent object & it’s handling protocol when

    we define a custom intent in intent.definition file. Output of Intent Definition File
  40. Handling Intent • To handle any custom intent, we need

    to implement the respective handling protocol. • Provide data for parameters whose options are dynamic • Resolve parameters (Getting value for parameters) • Once the parameters are resolved or once we have all the values, we need to handle or complete the intent. • What does handling protocol consists? What are the things that we need to implement?
  41. Handling Protocol

  42. Handling Protocol We need to implement all these functions. But

    which gets called first?
  43. Resolve Parameters Lifecycle Of Handler A final validation. By default,

    it’s valid. Resolve all parameters Execute
  44. Parameter Resolution

  45. • needsValue - if the value is nil and it

    is required, tell siri that you need value from user • confirmationRequired - siri will ask user for the confirmation of the parameter value • notRequired - value for parameter is not required, move on to the next parameter • success - valid value, move on to next parameter • unsupported - if the value is not supported, tell siri to ask question again • disambiguation - Siri will ask user to choose from the list of values provided with disambiguation. With this, you can provide or change the datasource.
  46. 4. Returns Valid Options 7. Converts the said or selected

    value to the expected parameter Lifecycle of Parameter Resolution Resolve Parmeter A SiriKit 1. Calls resolve initially 2. If value nil call needsValue() 5. Will ask user for the value. Provide Options A 3. For dynamic options 6. Selects or Says the value 8. Checks for the updated value in intent, return appropriate parameter resolution type Resolve Parmeter B 9c. Success or notRequired 9a. Unsupported or Disambiguation Resolution Type 9b. confirmationRequired 9b. If user confirmed
  47. Parameters are resolved when we return success or notRequired. Any

    other resolution result, will interact with the user again to get the value.
  48. Resolve Fund

  49. Resolve Amount

  50. Resolve Amount

  51. Handle

  52. Done with the handling

  53. Provide Handler • Now we need to provide our handler

    to sirikit. • Create Intent Extension target • Xcode creates a IntentHandler.swift file inside intent extension target for us when the target is created • Intent Handler file is where all intents are handled.
  54. Intent Handler The object we return in handler function must

    confirm to the protocol created for that intent.
  55. Add your custom Intent to Siri knowledge system

  56. One last thing

  57. To enhance the user experience. We can actually show our

    custom UI according to the response code we return. • To show our custom UI, we need to create a new target - IntentUIExtension • Xcode automatically generates a view controller class (IntentViewController.swift), where we can add our own child view controller or simply a view. • We can provide different UI for different response codes
  58. None
  59. None
  60. Different View For Different Response Codes

  61. And…we are done. Let’s see what we have built.

  62. None
  63. Thank You!