【问题标题】:Can an INIntent property be given the value "Ask Each Time"?可以为 INIntent 属性赋予值“每次询问”吗?
【发布时间】:2020-02-16 00:48:00
【问题描述】:

假设我在.intentdefinition 文件中定义了一个名为FooIntent 的自定义意图。

它有一个参数bar,类型为Decimal,对于它:
- User can supply value in Siri and Shortcuts app 处于活动状态。
- Default Value0

其自动生成的类定义现在如下所示:

public class FooIntent: INIntent {
    @NSManaged public var bar: NSNumber?
}

如果我构造一个FooIntent,请将bar 设置为nil,并将其传递给INUIAddVoiceShortcutViewController

let intent = FooIntent()
intent.bar = nil
intent.suggestedInvocationPhrase = "Do foo"

let shortcut = INShortcut(intent: intent)

let viewController = INUIAddVoiceShortcutViewController(shortcut: shortcut!)
viewController.delegate = self
window?.rootViewController?.present(viewController, animated: true, completion: nil)

Add to Siri 模态出现时,bar 参数用默认值 0 填充。

如果在Add to Siri UI 中,我将bar 更改为Ask Each Time 并按下Add to Siri,则生成的委托方法调用会生成一个INVoiceShortcut 对象,其中包含nilnilbar

func addVoiceShortcutViewController(_ controller: INUIAddVoiceShortcutViewController, didFinishWith shortcut: INVoiceShortcut?, error: Error?) {
    if let intent = shortcut?.shortcut.intent as? FooIntent {
        print(intent.bar) // nil
    }
    controller.dismiss(animated: true, completion: nil)
}

如果我在 UI 中将 bar 设置为 Clipboard,也是如此。

如果我将它设置为一个值,例如42,然后intent.bar 正确返回42

既然nil 似乎代表一个参数的多个概念,那么Ask Each Time 的概念存储在快捷方式或意图对象中的哪里?

有什么我可以传递给INUIAddVoiceShortcutViewController 的东西,比如intent.bar = <Ask Each Time>

【问题讨论】:

    标签: ios swift sirishortcuts


    【解决方案1】:

    通常您在 IntentHanlder 中控制行为(是否继续要求用户输入特定参数)。例如,我实现了这种方式:

    FooIntentHandler: NSObject, FooIntentHandling {
    
        func resolveBar(for intent: FooIntent, with completion: @escaping (FooBarResolutionResult) -> Void) {
            if let bar = intent.bar as? NSNumber {
    
                var everythingIsFine = false
    
                // Any logic here.
                // You can even assign an invalid magic number in your INUIAddVoiceShortcutViewController
                // so that your compare it here to see if the user has input anything different.
    
                if everythingIsFine {
                    completion(FooBarResolutionResult.success(with: bar))
                } else {
                    // With .needsValue() Siri will always ask for user input.
                    completion(FooBarResolutionResult.needsValue())
                }
            } else {
                completion(FooBarResolutionResult.needsValue())
            }
        }
    }
    

    您还可以指定一个有效值作为初始值,在这种情况下,只要您在您的 resolve 方法中调用 FooBarResolutionResult.success(),Siri 就会接受该参数值(无需用户输入),如上所示。

    简而言之,Siri 调用此resolveBar() 来决定是否要求用户输入。

    【讨论】:

    • 我就此事与 Apple 的一位工程师进行了通信,他建议此解决方案是预期的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 2021-03-09
    • 2021-05-08
    • 1970-01-01
    相关资源
    最近更新 更多