已实现的iOS AppIntents在快捷方式中未显示。

9

我正在尝试测试目前在iOS16 Beta上的新AppIntents API。查看文档,实现似乎非常简单,但是在实现后,我没有在Shortcuts应用程序中看到我的AppIntent。设备正在运行iOS 16.0。

这是我实现AppIntent的方式:


import AppIntents

struct DoSomethingIntent: AppIntent {
    
    static var title: LocalizedStringResource = "This will do something"
    
    static var description = IntentDescription("Does something")
    
    func perform() async throws -> some PerformResult {
        return .finished(value: "Done")
    }
}


根据文档,快捷方式应该能够在我的应用程序安装后找到我的AppIntent,但我发现这并不是事实。有人知道我的实现缺少什么吗?

“签名”会被系统缓存。每当您对意图(或AppShortcutsProvider)进行更改时,您需要删除应用程序(Xcode -> Product -> Clean Build Folder…),退出并重新启动快捷方式应用程序(macOS),或者重新启动模拟器(iOS/iPadOS)。您可能需要重复这个步骤一两次以使系统注意到其缓存已过期。 - Grant Neufeld
不确定这是否是自测试版以来的更改,但您应该尝试将perform()操作更改为return .result()return .result(value: "Done") - alexkaessner
3个回答

10

AppIntents API是比较新的技术,行为有些奇怪,而且文档非常匮乏。

在我的情况下,我通过添加\(.applicationName)参数到短语中使它们能够工作。请尝试以下方法:

struct LibraryAppShorcuts: AppShortcutsProvider {
    @AppShortcutsBuilder static var appShortcuts: [AppShortcut] {
        AppShortcut(intent: DoSomethingIntent(), phrases: ["Do something with \(.applicationName)"])
    }
}

通过iOS 17 beta和Xcode 15.0 beta 4,我成功地让我的第一个意图出现了,只需按照上述指示添加applicationName即可。我不需要清理构建文件夹、删除应用程序、重新启动应用程序、快捷方式应用程序、我的Mac或iPad mini。 - bruce1337
理论上来说,这不是必须要做的!使用AppShortcutsProvider将会在Spotlight中添加快捷方式,并作为“建议”出现在快捷方式应用程序中。但是,如果您只想将快捷操作添加到快捷方式应用程序中,您不必这样做。 - alexkaessner

2
  • 首先,您需要通过xcode-select选择您的Xcode-beta.app

  • 清除派生数据

  • 关闭您的应用程序和快捷方式应用程序

  • 在您的代码中添加一个快捷方式库

struct LibraryAppShorcuts: AppShortcutsProvider {
    @AppShortcutsBuilder static var appShortcuts: [AppShortcut] {
        AppShortcut(intent: DoSomethingIntent(), phrases: ["My something phrase"])
    }
}
  • 构建

0

API有很多错误。使用旧的SiriKit Intents.intentdefinition文件(按CMD+N创建)来设置你的意图,使用专用UI,然后选择选项转换为应用程序意图以生成实际工作的App意图,并根据需要进行调整。 意图定义文件

然后在快捷方式提供者中使用这些意图,并记得用.applicationName插值短语,否则它不会出现在快捷方式应用程序中。

struct Shortcuts: AppShortcutsProvider {

static var shortcutTileColor: ShortcutTileColor = .navy

static var appShortcuts: [AppShortcut] {
    AppShortcut(intent: SomeIntent(),
                phrases: ["Do something with \(.applicationName)"],
                shortTitle: "Just do it",
                systemImageName: "magnifyingglass")
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接