本文介绍了可以为INIntent属性赋予值“每次询问"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let's say I've defined a custom intent, called FooIntent, in an .intentdefinition file.

它具有类型为Decimal的单个参数bar,其参数为:
-User can supply value in Siri and Shortcuts app处于活动状态.
-Default Value0.

It has a single parameter, bar, of type Decimal, for which:
- User can supply value in Siri and Shortcuts app is active.
- Default Value is 0.

其自动生成的类定义现在看起来像这样:

Its autogenerated class definition now looks like this:

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

如果我构造一个FooIntent,请将bar设置为nil,然后将其传递到INUIAddVoiceShortcutViewController:

If I construct a FooIntent, set bar to nil, and pass it into 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)

出现Add to Siri模态,并用0(默认值)填充bar参数.

the Add to Siri modal appears with the bar parameter filled in with 0, its default value.

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

If, in the Add to Siri UI, I then change bar to Ask Each Time and press Add to Siri, the resulting delegate method call produces an INVoiceShortcut object that contains a nil value for 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)
}

如果我在用户界面中将bar设置为Clipboard,则同样如此.

The same is true if I set bar to Clipboard in the UI.

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

If I set it to a value, e.g. 42, then intent.bar correctly returns 42.

由于nil似乎代表一个参数的多个概念,因此Ask Each Time的概念存储在快捷方式或意图对象中的什么位置?

Since nil seems to represent multiple concepts for a parameter, where is the concept of Ask Each Time stored in the shortcut or intent objects?

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

Is there anything I can pass into INUIAddVoiceShortcutViewController such that intent.bar = <Ask Each Time>?

推荐答案

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

Usually you control the behavior (of whether or not to continue asking for user input for a particular param) in your IntentHanlder. For example I achieved to do it this way:

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就会接受参数值(不要求用户输入).

You can also assign a valid value as the initial one, in which case Siri will accept the param value (without asking for user input) as long as you call FooBarResolutionResult.success() in your resolve method as shown above.

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

In short, Siri calls this resolveBar() to decide whether or not asking for user input.

这篇关于可以为INIntent属性赋予值“每次询问"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:36