Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly implement siri shortcut

I'm implementing Siri Shortcut. My objective is add shortcuts of functions to SIRI SHORTCUT on Settings app.

In order to do so, I need to first register NSUserActivity & then handle the shortcuts calls from app delegate.

The problem is I'm not sure where and how to register the activity properly from a tutorail I watched.

Where should I register?

According to the tutorial I added this code on a view controller.

Since self.userActivity is accessible from app delegete I'm wondering if I could add the code below on app delegate.

func registerShortcut() {

        if #available(iOS 12.0, *) {
            let activity = NSUserActivity(activityType: "jp.co.mycompany.MyApp.openCamera")
            activity.title = "Camera Shortcut"
            activity.isEligibleForSearch = true
            activity.isEligibleForPrediction = true

            self.userActivity = activity
            self.userActivity?.becomeCurrent()
        } 
    }

How many times should I register

Without controlling how many times the registration code is called, app will call the registration code everytime app is launched. Is calling the registration multiple times causes any problem?

like image 454
Bigair Avatar asked Nov 01 '18 01:11

Bigair


People also ask

How do you code an Apple shortcut?

From an iPhone or iPad, open the Shortcuts app and tap the Automation tab. Here, you can select Create Personal Automation to build a shortcut that runs directly on your Apple device or Create Home Automation for one that runs for everyone in a household through a smart home device.


1 Answers

There are many ways to implement Shortcuts and Siri Suggestions in your app, so long as they follow the Human interface Guidelines set by apple. you should't be to concerned about implementation. I believe you would like to register for a action donation to Siri Shortcuts.

Take a dive into Apple's Example Code for programming Siri Shortcuts. I believe this the best resource on the matter. This code is very detailed and well documented.

"How many times should I register?"

"Where should I register?"

You should register your donation every time the user performs the action:

Donating the intent each time the user performs an action helps Siri learn about the user’s behavior, which helps Siri better predicate when the user may want to perform that action again.

So in your case, I wouldn't register the donation in the AppDelegate, instead I'd do it when a button is pressed or the user does some other action like opening the camera. Most often I see developer's group actions into a single file to manage them better.. Then call them when their respective actions are activated by the user.

like image 191
RLoniello Avatar answered Sep 20 '22 16:09

RLoniello