快速入门
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-share-target`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/share-target/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
“安装”章节你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下 prompt:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-share-target` plugin in my project.如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:
bun add @capgo/capacitor-share-targetbunx cap sync导入
“导入”章节import { CapacitorShareTarget } from '@capgo/capacitor-share-target';提前监听 (冷启动)
章节标题:“尽早监听(冷启动)”注册 shareReceived 在应用启动后立即进行,在用户进入处理共享内容的页面之前。在 iOS 上,原生层会保留该事件直到 JavaScript 处理它,这涵盖了 WebView 仍在启动期间到达的分享请求。
import { CapacitorShareTarget } from '@capgo/capacitor-share-target';
const shareListener = await CapacitorShareTarget.addListener('shareReceived', (event) => { routeSharedPayload(event.title, event.texts, event.files);});
// When tearing down the handler:await shareListener.remove();配置 appGroupId 中的 capacitor.config 并在你的 Share Extension 中配置相同的 App Group。请参阅 GitHub 上的插件 README 以获取扩展示例 code。
常见坑点
章节标题为“常见陷阱”- 原生配置:intent 仅在完成 README 中的 manifest 和 Xcode 配置步骤后生效。
- 冷启动:在路由器就绪前缓冲分享内容。
- 文件 URI:在 upload 前需先复制到应用存储中,否则访问权限可能会过期。
API 概览
章节标题:“API 概览”addListener
标题为“addListener”的章节监听 shareReceived 事件。
注册一个监听器,当内容从其他应用分享到本应用时,该监听器将被调用。回调函数会接收包含 title、texts 和 files 的 event 数据。
import { CapacitorShareTarget } from '@capgo/capacitor-share-target';
const listener = await CapacitorShareTarget.addListener('shareReceived', (event) => { console.log('Title:', event.title); console.log('Texts:', event.texts); event.files?.forEach(file => { console.log(`File: ${file.name} (${file.mimeType})`); });});
// To remove the listener:await listener.remove();removeAllListeners
标题为 “removeAllListeners” 的章节移除该插件的所有监听器。
import { CapacitorShareTarget } from '@capgo/capacitor-share-target';
await CapacitorShareTarget.removeAllListeners();getPluginVersion
“getPluginVersion” 章节获取原生 Capacitor 插件版本。
返回原生插件实现的当前版本。
import { CapacitorShareTarget } from '@capgo/capacitor-share-target';
const { version} = await CapacitorShareTarget.getPluginVersion();console.log('Plugin version:', version);类型参考
“类型参考” 章节ShareReceivedEvent
“ShareReceivedEvent” 章节内容分享至应用时接收到的事件数据。
export interface ShareReceivedEvent { /** * The title of the shared content. * * @since 0.1.0 */ title: string;
/** * Array of text content shared to the application. * * @since 0.1.0 */ texts: string[];
/** * Array of files shared to the application. * * @since 0.2.0 */ files: SharedFile[];}SharedFile
“SharedFile” 章节表示分享至应用的文件。
export interface SharedFile { /** * The URI of the shared file. On Android/iOS this will be a file path or data URL. * On web this will be a cached URL accessible via fetch. * * @since 0.1.0 */ uri: string;
/** * The name of the shared file, with or without extension. * * @since 0.1.0 */ name: string;
/** * The MIME type of the shared file. * * @since 0.1.0 */ mimeType: string;}单一事实来源
标题为“单一事实来源”的部分本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。
从入门指南继续
“从入门指南继续”章节如果你正在使用 快速入门 要规划 dashboard 和 API 操作,请将其连接到 使用 @capgo/capacitor-share-target 用于“使用 @capgo/capacitor-share-target”中的原生能力, API 概览 关于 API Overview 中的实现细节, 简介 关于 Introduction 中的实现细节, API 密钥 有关 API Keys 的实现细节,以及 设备 有关 Devices 的实现细节。