swiftxcodewatchossirikit

Shortcut/Action on Siri Watch Face with independent WatchOS app


I'm relatively new to making apps, and even newer to independent watch apps. As training, I'm making a watch app that I can use to log my water intake throughout the day. I've created a new intent definition file (see image 1) on which I've checked the marks for all the target memberships (the app, the WatchKit app, and the WatchKit extension). Furthermore, the target membership class is a public intent for the WatchKit extension.

When logging my water I execute the following code:

let intent = INManager.intent(drink: item)
INManager.donateShortcuts(withIntent: intent)

and my IntentManager looks like this:

import Foundation
import Intents

class IntentManager {
    
    func intent(drink: Drink) -> LogDrinkIntent {
        let intent = LogDrinkIntent()
        
        intent.uuid = drink.id.uuidString
        intent.name = drink.name
        intent.emoji = drink.emoji
        
        return intent
    }
    
    func donateShortcuts(withIntent intent:INIntent) {
        var relevantShortcuts: [INRelevantShortcut] = []
        
        if let relevantShortcut = defaultRelevantShortcut(withIntent: intent) {
            relevantShortcuts.append(relevantShortcut)
        }
        
        INRelevantShortcutStore.default.setRelevantShortcuts(relevantShortcuts) { (error) in
            if let error = error {
                print("Failed to set relevant shortcuts: \(error))")
            } else {
                print("Relevant shortcuts set.")
            }
        }
    }
    
    private func defaultRelevantShortcut(withIntent intent: INIntent) -> INRelevantShortcut? {
        if let shortcut = INShortcut(intent: intent) {
            let relevantShortcut = INRelevantShortcut(shortcut: shortcut)
            relevantShortcut.shortcutRole = .action
            
            let template = INDefaultCardTemplate(title: "Log Drink")
            relevantShortcut.watchTemplate = template

            print("Returning relevant shortcut.")
            
            return relevantShortcut
        }
        
        return nil
    }
}

When logging a drink the confirmation Returning relevant shortcut. and Relevant shortcuts set. are printed. However, the Siri watch face doesn't update to include a link to my action. I got the code for the IntentManager from this Medium article.

I really appreciate your time and help. I've had a hard time trying to find any details about this functionality and Apple's documentation is imo inferior. Thank you! If you need more details or such, feel free to ask.

Image 1 Intent definition file


Solution

  • Let's saddle the horse from behind: Generally speaking, you want to make use of Soup Chef. Now you can categorize Siri suggestions into two sub-groups, being donated shortcuts and relevant shortcuts.

    In your specific example of a "water intake" logging app, you should work with donating the INIntent to INIteraction. The reason for that is quite simple: Your suggestion is due to an action a user has committed within your application, not based upon plainly relevance, thus your passage about INRelevantShortcutStore isn't necessary and/or should be replaced with INInteraction.

    To re-phrase myself: The issue is that you parse INRelevantShortcutStore as a donation, see here:

    func donateShortcuts(withIntent intent:INIntent) {
            var relevantShortcuts: [INRelevantShortcut] = []
            
            if let relevantShortcut = defaultRelevantShortcut(withIntent: intent) {
                relevantShortcuts.append(relevantShortcut)
            }
            
            INRelevantShortcutStore.default.setRelevantShortcuts(relevantShortcuts) { (error) in
                if let error = error {
                    print("Failed to set relevant shortcuts: \(error))")
                } else {
                    print("Relevant shortcuts set.")
                }
            }
        }
    

    ... as explained above, that is not the correct usage for INIntent in your specific example.

    I highly suggest to read through Soup Chef in general as well as specifically donating shortcuts to Siri (what you want to do!). The documentation is very detailed and explanative!