Search code examples
iosswiftsiriswift4.2nsuseractivity

How to predefine certain phrases that should be used to trigger certain actions by Siri


I am able to create a shortcut using NSUserActivity or Intent, and then let the user to record a certain phrase which will trigger certain shortcut, by presenting to him an appropriate view controller, or by sending hi to the Settings->Siri->Shortcuts. For this, I would use just this code for example:

let activityType = PageID.page1.rawValue
    lazy var activity: NSUserActivity = {
        let userActivity = NSUserActivity(activityType: activityType)
        userActivity.title = "View Page"
        userActivity.suggestedInvocationPhrase = "Page one"
        userActivity.isEligibleForSearch = true
        userActivity.isEligibleForPrediction = true
        userActivity.persistentIdentifier =  NSUserActivityPersistentIdentifier(activityType)
        return userActivity
    }()

This just add a shortcut to Siri's suggestions and it makes it available in Settings, but still a user has to record phrase to trigger a shortcut by a certain phrase.

How to predefine phrases like "Do some task" for a specific action and make possible for a user to use it, but without having a need of manually recording it?

More precisely, I want to open a specific page in my app, based on what user says. For example, Open page one, Open page two, Open Menu etc...


Solution

  • What you want to do isn't currently possible with Apple's frameworks in iOS 12. Apple does allow your app to use Siri without the user recording phrases for very specific domains (e.g. messaging or payments), but not arbitrary tasks. I see Siri Shortcuts as a first step towards that. It allows you to provide the system with tasks your app is capable of and then requires the user to record phrases that trigger those tasks.