已配置AppIntent与AppShortcutsProvider,iOS快捷方式未在Spotlight显示
我正尝试为一款小型iOS应用添加快捷方式,原本以为只要实现Intent、添加AppShortcutsProvider就够了,但在设备上运行后,iOS Spotlight搜索应用名称时看不到该快捷方式。请问我遗漏了什么配置?
以下是相关代码:
import AppIntents struct TestIntent: AppIntent { static var title: LocalizedStringResource = "Test Intent" static var description: LocalizedStringResource = "Test Intent" static var openAppWhenRun: Bool = false @Parameter(title: "Test Parameter") var testParameter: String? @MainActor func perform() async throws -> some ProvidesDialog { guard let testParameter else { return .result(dialog: "Please provide a valid test parameter.") } return .result(dialog: "Test parameter is: \(testParameter).") } } struct TestIntentShortcut: AppShortcutsProvider { static var appShortcuts: [AppShortcut] { AppShortcut( intent: TestIntent(), phrases: [ "Set parameter to \(\.$testParameter) in \(.applicationName)." ], shortTitle: "Test Shortcut", systemImageName: "questionmark.text.page" ) } }
你遗漏了这些关键配置和步骤:
注册AppShortcutsProvider
SwiftUI应用需在主App结构体的body中添加.appShortcuts(TestIntentShortcut())修饰符,让系统识别你的快捷配置;UIKit应用则需在AppDelegate或SceneDelegate中完成注册,否则系统无法检测到快捷方式。完善本地化支持
LocalizedStringResource对应的文本需要在Localizable.strings文件中配置本地化内容,缺失本地化会导致Spotlight无法正确加载快捷方式的标题和描述,进而不展示。调整参数配置
可选参数testParameter: String?会让Spotlight难以生成可直接触发的快捷方式,建议将参数设为必填,或添加默认值,确保快捷方式有明确的执行逻辑。重新安装并等待索引
Spotlight需要时间索引新快捷方式,先删除应用重新安装,之后等待1-2分钟再搜索;同时检查设备设置:进入「设置 -> Siri与搜索 -> 你的应用」,确认「从App中搜索」已开启。验证Intent执行逻辑
确保TestIntent的perform方法能稳定执行,无未处理的错误;虽然openAppWhenRun设为false没问题,但要保证Intent无需依赖应用启动也能完成核心逻辑。
内容的提问来源于stack exchange,提问作者appfrosch

