跳过主要内容
返回插件
@capgo/capacitor-persistent-uuid
教程
@capgo/capacitor-persistent-uuid

UUID持久

在重新安装、应用程序更新和操作系统更新时,生成并持久化一个应用程序范围的UUID

指南

关于持久UUID的教程

使用 @capgo/capacitor-persistent-uuid

为 Capacitor 生成一个持久的应用程序范围的UUID,并在重新安装和更新流程中保持稳定,正常应用程序存储被删除时

安装

npm install @capgo/capacitor-persistent-uuid
npx cap sync

What This Plugin Exposes

  • getId - 读取存储的 UUID,若无则为选定范围创建一个 UUID。
  • resetId - 重新生成 UUID,用于注销、隐私重置或测试清理流程.
  • scope - 选项性命名空间,用于包名或自定义范围不同但 debug 和生产包标识符相同的 app。

Example Usage

import { PersistentUuid } from '@capgo/capacitor-persistent-uuid';

const { id, created, scope } = await PersistentUuid.getId();

console.log(id);
console.log(created);
console.log(scope);

Share One UUID Across Build Variants

const result = await PersistentUuid.getId({
  scope: 'com.example.app',
});

console.log(result.id);

Reset The Identifier

const replacement = await PersistentUuid.resetId();
console.log(replacement.id);

Platform Notes

Android 将 UUID 存储在 AccountManager 中,以便在 Android Studio reinstall 周期和 debug vs Play 签名差异时包名或自定义范围稳定时 UUID 可以存活。 iOS 将 UUID 存储在 Keychain 中,并且在 Keychain 访问兼容的情况下可以通过 app 和 iOS 更新保持存活。 Web 使用 localStorage 作为开发fallback。

This plugin does not expose a hardware ID and does not survive factory reset, manual account removal, Keychain clearing, browser storage clearing, or resetId.

Full Reference

继续前进

如果您正在使用 @capgo/capacitor-persistent-uuid 来保持应用程序身份稳定,连接它与 @capgo/capacitor-persistent-uuid 概述, 入门 安装和API示例的 安卓行为 重新安装详细信息, iOS 行为 为 Keychain 详情,和 @capgo/capacitor-持久账户 当您需要账户数据持久化而不是 UUID 时。