指南
如何在 Capacitor 中使用 App Intents
使用 @capgo/capacitor-app-intents
Capgo App Intents 插件接口。
安装
bun add @capgo/capacitor-app-intents
bunx cap sync
bunx cap sync 将插件的原生 code 复制到你的原生项目中。每次升级插件后都要重新运行。
导入
import { CapgoAppIntents } from '@capgo/capacitor-app-intents';
API 概览
| 方法 | 描述 |
|---|---|
getPendingInvocations |
返回在 JavaScript 就绪前已排队的调用。 |
removePendingInvocation |
在 handler 处理完后,移除一个待处理的调用。 |
setEntityCatalog |
为给定的类型字符串持久化实体目录。 |
setHomeScreenShortcutItems |
替换主屏幕快速操作 (UIApplicationShortcutItems)。 |
indexSpotlightEntities |
为你的 App 域建立 Spotlight 条目索引。 |
removeSpotlightEntities |
按 id 删除 Spotlight 项。 |
performAppleSchemaTemplate |
将 JavaScript 处理程序加入队列,用于 alpha 版 Apple schema template。 |
示例
getPendingInvocations()
返回在 JavaScript 就绪前已排队的调用。
import { CapgoAppIntents } from '@capgo/capacitor-app-intents';
const result = await CapgoAppIntents.getPendingInvocations();
console.log(result);
removePendingInvocation()
在 handler 处理完后,移除一个待处理的调用。
import { CapgoAppIntents } from '@capgo/capacitor-app-intents';
await CapgoAppIntents.removePendingInvocation({ id: 'id-123' });
setEntityCatalog()
为给定的类型字符串持久化实体目录。
import { CapgoAppIntents } from '@capgo/capacitor-app-intents';
await CapgoAppIntents.setEntityCatalog({
type: 'type',
entries: [
{
id: 'id-123',
title: 'Hello from Capacitor',
},
],
});
setHomeScreenShortcutItems()
替换主屏幕快速操作 (UIApplicationShortcutItems)。
import { CapgoAppIntents } from '@capgo/capacitor-app-intents';
await CapgoAppIntents.setHomeScreenShortcutItems({
items: [
{
type: 'type',
title: 'Hello from Capacitor',
},
],
});
indexSpotlightEntities()
为你的 App 域建立 Spotlight 条目索引。
import { CapgoAppIntents } from '@capgo/capacitor-app-intents';
await CapgoAppIntents.indexSpotlightEntities({
entities: [
{
id: 'id-123',
title: 'Hello from Capacitor',
actionName: 'action',
},
],
});
removeSpotlightEntities()
按 id 删除 Spotlight 项。
import { CapgoAppIntents } from '@capgo/capacitor-app-intents';
await CapgoAppIntents.removeSpotlightEntities({ ids: ['ids'] });
上表列出了 7 个核心方法。有关监听器、版本辅助函数以及每个方法的完整契约,请参阅 GitHub 仓库.
监听事件
addListener 会返回一个句柄。调用 handle.remove() 在屏幕卸载时,或者调用 CapgoAppIntents.removeAllListeners() 以清除所有监听器。