可以为 INIntent 属性赋予值“每次询问”吗?

Naf*_*der 5 ios swift sirishortcuts

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

它有一个bar类型Decimal为的单个参数,其中:
-User can supply value in Siri and Shortcuts app是活动的。
-Default Value0

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

public class FooIntent: INIntent {
    @NSManaged public var bar: NSNumber?
}
Run Code Online (Sandbox Code Playgroud)

如果我构造 a FooIntent,设置barnil,并将其传递给 a 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)
Run Code Online (Sandbox Code Playgroud)

Add to Siri与模态出现bar参数填满了0,它的默认值。

如果在Add to SiriUI 中,我更改barAsk Each Time并按下Add to Siri,则生成的委托方法调用会生成一个INVoiceShortcut包含以下nil值的对象bar

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)
}
Run Code Online (Sandbox Code Playgroud)

如果我在 UI 中设置barClipboard,也是如此。

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

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

有什么我能进入INUIAddVoiceShortcutViewController这样intent.bar = <Ask Each Time>

Wei*_*ANG 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())
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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