快速入门
复制包含安装步骤和该插件完整 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-persistent-uuid`
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/persistent-uuid/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 工具中:
bunx 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-persistent-uuid` plugin in my project.如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:
bun add @capgo/capacitor-persistent-uuidbunx cap sync快速入门
章节标题为“快速入门”import { PersistentUuid } from '@capgo/capacitor-persistent-uuid';
const result = await PersistentUuid.getId();console.log(result);示例:resetId
标题为“示例:resetId”的章节import { PersistentUuid } from '@capgo/capacitor-persistent-uuid';
const result = await PersistentUuid.resetId();console.log(result);常见坑点
章节标题为“常见陷阱”- 原生同步:运行
bunx cap sync在插件升级后。 - 唯一可信来源:信任
src/definitions.ts在 GitHub 仓库中。
导入
“导入”章节import { PersistentUuid } from '@capgo/capacitor-persistent-uuid';读取或创建 UUID
标题为“读取或创建 UUID”的章节import { PersistentUuid } from '@capgo/capacitor-persistent-uuid';
const result = await PersistentUuid.getId();
console.log(result.id);console.log(result.scope);console.log(result.created);首次调用会创建并存储一个 UUID。后续针对相同 scope 的调用将返回同一个 UUID。
使用稳定的自定义 scope
使用稳定的自定义 Scope原生默认 scope 在 Android 上是包名,在 iOS 上是 bundle identifier。如果 debug 和 production 构建使用不同的包标识符,但需要共享同一个 UUID,请传入一个共享的 scope。
const result = await PersistentUuid.getId({ scope: 'com.example.app',});重置 UUID
标题为“重置 UUID”的章节当用户登出、请求隐私重置,或自动化测试需要新标识符时,调用 resetId。
const replacement = await PersistentUuid.resetId();console.log(replacement.id);持久化预期
标题为“持久化预期”的章节- 只要包名或自定义 scope 保持稳定,Android 即使在卸载重装后(包括使用不同签名密钥的 Android Studio 和 Play 安装)也能保留 UUID。
- 只要 Keychain 访问规则保持不变,iOS 在应用更新和 iOS 系统更新后都能保留 UUID。
- Web 使用 localStorage,仅作为开发阶段的回退方案。
从入门指南继续
“从入门指南继续”章节如果你正在使用 快速入门 若要添加持久化应用身份,请将其连接至 @capgo/capacitor-persistent-uuid 查看概览, Android 行为 查看重新安装行为, iOS 行为 查看 Keychain 行为, 使用 @capgo/capacitor-persistent-uuid 用于教程,以及 Capgo 插件目录 用于其他原生插件。