【发布时间】:2019-12-22 02:42:44
【问题描述】:
我正在尝试将 Siri Shortcuts 集成到我的应用程序中。我正在尝试的概念是通过秘密密码确认来获得我的卡的奖励积分。请在下面找到我为此做了什么。
- 在功能中启用了 Siri,并添加了 Siri Intent 定义文件。
添加了名为 say Rewards 的新自定义意图。
定义了标题。启用确认的字幕和参数(accType,pin)。 Pin 将单独发送给用户。
- 然后使用参数“rewardPoints”定义意图响应并定义响应消息。
- 添加了 Siri 意图扩展。
- 在项目和意图扩展中的 info.plist 文件中添加了自定义意图。
- 已验证并为自定义意图添加了新的处理程序,并定义了解析、处理和确认方法,如下所示。目前,我会随机拒绝奖励积分。
//
// RewardsIntentHandler.swift
// SiriIntentExt
//
import UIKit
import Intents
class RewardsIntentHandler: NSObject, RewardsIntentHandling {
func resolveAccType(for intent:RewardsIntent, with completion: @escaping ([INStringResolutionResult]) -> Void) {
guard let accType = intent.accType else {
completion([INStringResolutionResult.needsValue()])
return
}
completion([INStringResolutionResult.success(with: accType)])
}
func resolvePin(for intent:RewardsIntent, with completion: @escaping ([INIntegerResolutionResult]) -> Void) {
guard let verifyPin = intent.pin else {
completion([INIntegerResolutionResult.needsValue()])
return
}
completion([INIntegerResolutionResult.confirmationRequired(with: verifyPin as? Int)])
}
func confirm(intent: RewardsIntent, completion: @escaping (RewardsIntentResponse) -> Void) {
completion(RewardsIntentResponse.init(code: RewardsIntentResponseCode.ready, userActivity: nil))
}
func handle(intent: RewardsIntent, completion: @escaping (RewardsIntentResponse) -> Void) {
guard intent.accType != nil else {
completion(RewardsIntentResponse.init(code: RewardsIntentResponseCode.continueInApp, userActivity: nil))
return
}
guard intent.pin != nil else {
completion(RewardsIntentResponse.init(code: RewardsIntentResponseCode.continueInApp, userActivity: nil))
return
}
let response = RewardsIntentResponse.success(rewardPoints: NSNumber(value: 3453))
completion(response)
}
}
- 修改了 IntentHandler 以返回奖励意图的奖励处理程序
//
// IntentHandler.swift
// SiriIntentExt
//
import Intents
class IntentHandler: INExtension {
override func handler(for intent: INIntent) -> Any {
if intent is RewardsIntent {
return RewardsIntentHandler()
}
return self
}
}
- 捐赠的视图加载意图如下。
//
// ViewController.swift
// Shortcuts
//
import UIKit
import Intents
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
siriAuthorisarion()
donateRewardIntent()
}
func siriAuthorisarion() {
INPreferences.requestSiriAuthorization { (status) in
print("Siri Authorization Status - ", status)
}
}
func donateRewardIntent() {
let rewardsIntent = RewardsIntent()
rewardsIntent.suggestedInvocationPhrase = "Reward Points"
rewardsIntent.accType = "test account"
let interaction = INInteraction(intent: rewardsIntent, response: nil)
interaction.donate { error in
if let error = error {
print("Donating intent failed with error \(error)")
}
DispatchQueue.main.async {
let alert = UIAlertController.init(title: ((error != nil) ? "Error" : "Success"), message: ((error != nil) ? "Oops!!! Error occured on donating intent." : "Intent donated succussfully!!!"), preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}
}
我遇到了上述代码库的问题。 Siri 不要求输入密码,也无法获得该帐户的确切奖励积分。
有以下问题。
- 我们能否以编程方式将意图添加到 Siri,而不是从快捷方式应用程序或设置中添加。这样用户就可以在安装应用程序后直接使用该功能。
- 使用 Shortcuts 应用程序添加意图后,我正在尝试向 Siri 询问奖励积分。它立即请求定义我的应用程序快捷方式。一旦我们对请求说“是”,我需要被要求提供密码。但 Siri 回复我的应用程序有一些问题。询问下一个参数值要做什么。
- 在处理程序文件中,我为每个参数添加了解析方法。我觉得,解决方法没有被调用来验证值。我们是否需要处理任何事情才能使解析方法起作用?
- 如何在解析/处理/确认方法中使用断点调试处理程序实现。
提前致谢。
【问题讨论】:
标签: ios swift siri sirikit sirishortcuts