2

I am implementing iOS16 App Shortcuts. I am using SiriTipView to show users the possibilities.

My AddItem intent has a parameter for which Box entity to add the Item to. The shortcut will prompt for this.

@available(iOS 16.0, *)
struct MyShortcuts: AppShortcutsProvider { 
    @AppShortcutsBuilder static var appShortcuts: [AppShortcut] {  
        AppShortcut(intent: AddItem(), phrases: [
            "Add new item to \(\.$box) in \(.applicationName)",
            "Add new item in \(.applicationName)",
        ],
            shortTitle: "Add New Item",
            systemImageName: "pills"
        )
    }
}

I would like to add an example Box (from the user's existing data, like I do for the disambiguation) to the SiriTipView.

Currently, the tip view does not fill in the entity placeholder:

"Add new item to ${box} in <My App Name>"

The tip view is defined like this.

SiriTipView(intent: AddItem())

I realise I could just change the topmost phrase and have it use the phrase without the parameter, but I think it would be useful to the user to see that they can speak the box parameter.

I tried initialising the tip view with an IntentParameter like this:

SiriTipView(intent: AddItem(box: IntentParameter<BoxAppEntity>))

But I could not figure out how to just give it an example box entity. I guess I need to know how to initialise an IntentParameter with a concrete entity.

Chris
  • 4,009
  • 3
  • 21
  • 52

3 Answers3

2

The answer by M Y led me in the right direction.

I found that with many of the IntentParameter initialisers, the name of my concrete entity was being ignored, or there were compile time errors.

In the end, I had to set the wrappedValue like this.

func getIntentParameter() -> IntentParameter<BoxAppEntity?> {
    let parameter = IntentParameter<BoxAppEntity?>(title: "Example Parameter")
    parameter.wrappedValue = BoxAppEntity(id: "boxID", displayString: "Box Name")
    return parameter
}

Then I used this in the SiriTipView:

SiriTipView(intent: AddItem(box: getIntentParameter()))

In the getIntentParameter method I can simply set the displayString based on some arbitrary user data.

Chris
  • 4,009
  • 3
  • 21
  • 52
-1

To provide an example Box entity in the SiriTipView for your AddItem intent, you can create an instance of the IntentParameter with a concrete Box entity object. Here's how you can do it:

import IntentsUI

// Assuming Box is a custom entity class
let exampleBox = Box(name: "Example Box") // Create an example Box entity

let boxParameter = IntentParameter<BoxAppEntity>(
    name: "box",
    value: BoxAppEntity(box: exampleBox) // Wrap the example Box in a BoxAppEntity
)

let tipView = SiriTipView(intent: AddItem(box: boxParameter))

In the above code, you create an example Box object with the necessary data and then wrap it in a BoxAppEntity instance to satisfy the IntentParameter requirement. Finally, you can pass this boxParameter as a parameter to the AddItem intent while initializing the SiriTipView.

By providing an example Box entity in the SiriTipView, the user will be able to see the placeholder value and understand that they can speak the box parameter when using the app shortcuts.

M Y
  • 1,831
  • 4
  • 24
  • 52
-1

To initialize an IntentParameter with a concrete entity for the SiriTipView, you can create an instance of IntentParameter with the desired entity value. Here's an example of how you can achieve this:

swift
import Intents

let exampleBoxEntity = BoxAppEntity()
exampleBoxEntity.boxName = "Example Box"

let boxParameter = INParameter(intent: AddItemIntent.intentDefinition(), parameterKey: "box")
boxParameter.displayString = "Example Box"
boxParameter.defaultValue = exampleBoxEntity

let tipView = SiriTipView(intent: AddItemIntent(), parameters: [boxParameter])

In the code above, exampleBoxEntity is an instance of the BoxAppEntity with the desired value for the entity. You can set the necessary properties of exampleBoxEntity according to your data model. Then, an INParameter object is created with the intent definition (AddItemIntent.intentDefinition()) and the parameter key ("box"). The displayString property is set to "Example Box" to provide a user-friendly label for the example entity. Finally, the defaultValue of the parameter is set to exampleBoxEntity to associate the concrete entity value with the parameter. The boxParameter is passed as an element in the parameters array when initializing the SiriTipView with the AddItemIntent. This will ensure that the SiriTipView shows the example box entity in the placeholder for the Box parameter. Please note that the code assumes you have defined an AddItemIntent and a BoxAppEntity conforming to the necessary protocols and requirements for the SiriKit integration in your iOS app. Make sure to replace those with the appropriate definitions from your codebase.

Mukesh
  • 63
  • 1
  • 8