React Native iOS模块:如何向快捷指令App捐赠App Intent?
问题:如何自动捐赠App Intent作为快捷指令?
我开发了一款React Native应用,当前重点开发iOS模块。最初通过Intent定义文件创建Intent,之后将其转换为App Intent。目前手动在快捷指令App中搜索应用并添加带模板的Intent可正常工作:添加后Siri会请求参数,启动应用后我能在Swift/OC代码中接收用户输入的参数。
我的需求是:如何/在哪里捐赠该Intent作为快捷指令,让它自动出现在快捷指令App中并被Siri识别,无需用户手动操作?
以下是转换后生成的App Intent代码:
import Foundation import AppIntents import UIKit @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) struct [AppName]Command: AppIntent, CustomIntentMigratedAppIntent, PredictableIntent { static let intentClassName = "[AppName]CommandIntent" static var openAppWhenRun = true static var title: LocalizedStringResource = "[AppName] Command" static var description = IntentDescription("A command should be done by [AppName]") @Parameter(title: "Command") var command: String? static var parameterSummary: some ParameterSummary { Summary("\(\.$command) by [AppName]") } static var predictionConfiguration: some IntentPredictionConfiguration { IntentPrediction(parameters: (\.$command)) { command in DisplayRepresentation( title: "\(command!) by [AppName]", subtitle: "The \(command!) should be done by [AppName]" ) } } func perform() async throws -> some IntentResult { print("SI.Command Performed: \(command ?? "No Command")") // Log for debugging NotificationCenter.default.post(name: Notification.Name("[AppName]CommandTriggered"), object: nil, userInfo: ["command": command ?? ""]) // Example feedback to Siri return .result() } } @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) fileprivate extension IntentDialog { static var commandParameterConfiguration: Self { "Work orders, Assets, Invoices" } static func commandParameterPrompt(command: String) -> Self { "What is your \(command) for [AppName]?" } static var responseSuccess: Self { "Done" } }
解决方案
要让你的App Intent自动出现在快捷指令App中,需通过Intent捐赠实现,具体步骤如下:
1. 添加Intent捐赠扩展
在Xcode中给项目新增一个Intent Donation Extension目标:
- 路径:
File > New > Target,搜索并选择"Intent Donation Extension"。 - 这个扩展将负责在合适时机完成Intent的捐赠操作。
2. 实现捐赠逻辑
在扩展的主文件(默认是IntentDonationHandler.swift)中,编写捐赠[AppName]Command的代码:
import AppIntents class IntentDonationHandler: INExtension { override func handler(for intent: INIntent) -> Any { return self } // 执行Intent捐赠的方法 func donateCommandIntent() { guard #available(iOS 16.0, *) else { return } let commandIntent = [AppName]Command() // 可在这里预设参数,比如 commandIntent.command = "Work orders" // 创建捐赠请求 let donation = INDonation(intent: commandIntent, donationIdentifier: "[AppName]CommandDonation") // 发起捐赠 INInteraction.donate(donation) { error in if let error = error { print("Intent捐赠失败: \(error.localizedDescription)") } else { print("Intent捐赠成功") } } } }
3. 在主应用触发捐赠
选择应用启动后的合适时机(比如AppDelegate的didFinishLaunchingWithOptions,或SceneDelegate的sceneDidBecomeActive)调用捐赠方法:
import Intents // AppDelegate示例 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { guard #available(iOS 16.0, *) else { return true } let handler = IntentDonationHandler() handler.donateCommandIntent() return true }
4. 配置权限与扩展信息
- 主应用Info.plist:添加
NSSiriUsageDescription键,填写应用使用Siri的用途说明(例如"通过Siri快速触发[AppName]的命令操作")。 - Intent扩展Info.plist:在
NSExtension>NSExtensionAttributes下添加IntentsSupported数组,将[AppName]Command类名加入其中。
5. 优化Siri识别与建议
在[AppName]Command结构体中补充触发短语配置,提升Siri识别度:
@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) struct [AppName]Command: AppIntent, CustomIntentMigratedAppIntent, PredictableIntent { // 保留原有代码 // 添加Siri触发短语 static var suggestedInvocationPhrases: [LocalizedStringResource] { ["用[AppName]执行命令", "[AppName]处理\(\.$command)"] } }
完成以上配置后,应用首次启动时会自动捐赠Intent,该快捷指令将出现在快捷指令App的"我的快捷指令"或"建议"栏目中,用户无需手动搜索添加,同时Siri也能识别预设的触发短语。
内容的提问来源于stack exchange,提问作者Mo Hatami
相关产品推荐
相关产品推荐

