4

I want to advanced research Shortcuts technology. So here are some questions:

  • Can Siri shortcuts be used in any type of app? Because of SiriKit only working in tourism, chatting etc.
  • Now using shortcuts. Can I jump to my app showing specific page by Siri?
kgaidis
  • 14,259
  • 4
  • 79
  • 93
Corbin
  • 113
  • 1
  • 7

1 Answers1

14

Can Siri shortcuts be used in any type of app? Because of SiriKit only working in tourism, chatting etc.

Yes, any. They aren't tied to any specific domain. They are custom.

Now using shortcuts. Can I jump to my app showing specific page by Siri?

Yes.


The code below shows the easiest way on how to show a specific page by Siri, it's called "Donating a Shortcut" via NSUserActivity. But you could achieve the same by defining a custom INIntent.

Step 1:

In Info.plist, under NSUserActivityTypes add an activity type string: com.app.viewPage

Step 2:

Create activity:

let viewPageActivityType = "com.app.viewPage"

let viewPageActivity: NSUserActivity = {
    let userActivity = NSUserActivity(activityType: viewPageActivityType)
    userActivity.title = "View Page"
    userActivity.suggestedInvocationPhrase = "View Page"
    userActivity.isEligibleForSearch = true
    userActivity.isEligibleForPrediction = true
    return userActivity
}()

Then add it to your view controller:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        userActivity = viewPageActivity
    }
}

Step 3:

Handle it in UIApplicationDelegate method (this method will get called if a user presses the Shortcut, or activates in from Siri via voice):

public func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {

    if userActivity.activityType == viewPageActivityType {
        print("open the `ViewController` here")
        return true
    }
    return false
}

After the user opens the ViewController once, they may get it suggested in the lock screen and search suggestions. They can also go to Settings -> Siri & Search -> My Shortcuts to define a custom phrase to perform the action using voice.

To debug this, make sure to use a device (not simulator). Then go to Settings -> Developer -> Enable "Display Recent Shortcuts" and "Display Donations on Lock Screen".

There are a lot of great resources on this:

kgaidis
  • 14,259
  • 4
  • 79
  • 93
  • 1
    Your answer helped me a lot – Corbin Jun 07 '18 at 11:48
  • thanks,I read SoupChef demo, But I hava a question? I can't find OrderSoupIntent file in the project.And This OrderSoupIntent,There is a OrderSoupIntentHandling protocol,and I don't know who call this protocol method. – Corbin Jun 07 '18 at 15:24
  • @Corbin The files are automatically generated from `Intents.intentdefinition` file. The "OrderSoup" custom intent automatically generates a class + protocol. This can be customized via "Identity Inspector" in right panel in Xcode, under "Custom Class." You can just search where `OrderSoupIntentHandling` is used. It's used in `SoupChefIntents` target, `IntentHandler` class/file. – kgaidis Jun 07 '18 at 16:38
  • I found that the 'OrderSoupIntentHandler' class only wirtes the implementation of NewWordIntentHandling protocol method.So I hava a question?Where setup delegate?I use oc, but I don't know how to write xxx.delegate = self.If I only write the NewWordIntentHandling protocol, this method can't be call.Because I don't setup delegate . – Corbin Jun 08 '18 at 06:08
  • This is working really fine for me. But how is it possible to add multiple activities in this way? – mahega Sep 18 '18 at 14:08
  • @mh-itc yes you can create multiple activities and an app probably has multiple different `UIViewController`'s. If you want multiple different activities on the same view controller, I'm not sure u can do it using the code I posted, but you can roll a more custom solution by using custom `INIntent`. Short answer: yes, but probably not with this exact code. – kgaidis Sep 18 '18 at 15:16