【问题标题】:Facing issue in Siri Integration with custom intents在 Siri 集成中面临自定义意图的问题
【发布时间】:2019-12-22 02:42:44
【问题描述】:

我正在尝试将 Siri Shortcuts 集成到我的应用程序中。我正在尝试的概念是通过秘密密码确认来获得我的卡的奖励积分。请在下面找到我为此做了什么。

  1. 在功能中启用了 Siri,并添加了 Siri Intent 定义文件。
  2. 添加了名为 say Rewards 的新自定义意图。

  3. 定义了标题。启用确认的字幕和参数(accType,pin)。 Pin 将单独发送给用户。

  1. 然后使用参数“rewardPoints”定义意图响应并定义响应消息。

  1. 添加了 Siri 意图扩展。
  2. 在项目和意图扩展中的 info.plist 文件中添加了自定义意图。

  1. 已验证并为自定义意图添加了新的处理程序,并定义了解析、处理和确认方法,如下所示。目前,我会随机拒绝奖励积分。
//
//  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)
    }
}
  1. 修改了 IntentHandler 以返回奖励意图的奖励处理程序
//
//  IntentHandler.swift
//  SiriIntentExt
//

import Intents

class IntentHandler: INExtension {

    override func handler(for intent: INIntent) -> Any {

        if intent is RewardsIntent {
            return RewardsIntentHandler()
        }

        return self
    }

}
  1. 捐赠的视图加载意图如下。
//
//  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 不要求输入密码,也无法获得该帐户的确切奖励积分。

有以下问题。

  1. 我们能否以编程方式将意图添加到 Siri,而不是从快捷方式应用程序或设置中添加。这样用户就可以在安装应用程序后直接使用该功能。
  2. 使用 Shortcuts 应用程序添加意图后,我正在尝试向 Siri 询问奖励积分。它立即请求定义我的应用程序快捷方式。一旦我们对请求说“是”,我需要被要求提供密码。但 Siri 回复我的应用程序有一些问题。询问下一个参数值要做什么。
  3. 在处理程序文件中,我为每个参数添加了解析方法。我觉得,解决方法没有被调用来验证值。我们是否需要处理任何事情才能使解析方法起作用?
  4. 如何在解析/处理/确认方法中使用断点调试处理程序实现。

提前致谢。

【问题讨论】:

    标签: ios swift siri sirikit sirishortcuts


    【解决方案1】:

    找到我对上述问题的分析。

    1. 我们能否以编程方式将意图添加到 Siri,而不是从快捷方式应用程序或设置中添加。这样用户在安装应用程序后就可以直接使用该功能。

    By default, intents are provided for specific domains such as messaging, payments, photos, workout, etc. No need to explicitly add intents through shortcuts for theses specific domains. Apart from these domains if we are creating custom intent, we are in need to donate and add the intents to Siri using shortcut/settings application.

    1. 使用 Shortcuts 应用程序添加意图后,我正在尝试向 Siri 询问奖励积分。它立即请求定义我的应用程序快捷方式。一旦我们对请求说“是”,我就需要被要求提供密码。但 Siri 回答我的应用程序存在一些问题。要求下一个参数值该怎么做。

    From iOS13, Apple has added Siri parameters and Siri suggestion for custom intents to request the missing parameters. Till iOS12, we don't have parameters option for custom intents.

    1. 在处理程序文件中,我为每个参数添加了解析方法。我觉得,解决方法没有被调用来验证值。我们是否需要处理任何事情才能使解析方法起作用?

    In iOS12, we cannot add resolve methods for parameters in custom intents. Resolve methods handled only for specific domains provided within Intents extensions as mentioned in question 1. From iOS13, we can have resolve methods for custom intents based on the parameters.

    1. 如何在解析/处理/确认方法中使用断点调试处理程序实现。

    We can add breakpoints and debug intent handler methods.

    谢谢。

    【讨论】:

    • 我添加了快捷方式,但显示错误“出现问题app name 说“”“
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2021-10-14
    • 1970-01-01
    • 2017-01-21
    • 1970-01-01
    相关资源
    最近更新 更多