FCUUID
iOS UUID 库可以替代旧的良好 UDID 和 identifierForVendor。 该库提供了最简单的 API,以获取具有不同持久性级别的通用唯一标识符。
可以检索为同一用户的所有设备创建的 UUID,通过这种方式在服务器端的帮助下,可以轻松管理多个设备上的来宾帐户。
要求和依赖性
- iOS >= 5.0
- ARC enabled
- Key-value storage enabled (target / Capabilities / iCloud / Key-value storage)
- Security.framework
- UICKeyChainStore
- (optional) - Key-value storage enabled -> Target / Capabilities / iCloud / Key-value storage enabled if you want to share
uuidsOfUserDevices
values across multiple devices using the same iCloud account. - (optional) - KeyChain sharing enabled (entitlements and provisioning profile) if you need to share the same
uuidForDevice
/uuidsOfUserDevices
values across multiple apps with the same bundle seed.
安装
CocoaPods:
pod 'FCUUID'
手动安装:
- Copy
FCUUID
folder to your project. - Manual install UICKeyChainStore
可选设置:
建议在 applicationDidFinishLaunchingWithOptions 方法中进行设置。
将观察者添加到 FCUUIDsOfUserDevicesDidChangeNotification,以通知用户设备更改的 uuid。
如有必要,请使用 API 部分中列出的一种迁移方法从以前使用的 UUID 或 UDID 进行迁移(建议在调用 uuidForDevice 或 uuidsForUserDevices 方法之前进行迁移)。 请记住,仅当现有值是有效的 uuid 且尚未创建 uuidForDevice 时,迁移才起作用。
调用任何类方法以强制执行 iCloud 同步。
API
获取不同的UUID(每个都有自己的持久性级别)
//changes each time (no persistent) +(NSString *)uuid; //changes each time (no persistent), but allows to keep in memory more temporary uuids +(NSString *)uuidForKey:(id)key; //changes each time the app gets launched (persistent to session) +(NSString *)uuidForSession; //changes each time the app gets installed (persistent to installation) +(NSString *)uuidForInstallation; //changes each time all the apps of the same vendor are uninstalled (this works exactly as identifierForVendor) +(NSString *)uuidForVendor; //changes only on system reset, this is the best replacement to the good old udid (persistent to device) +(NSString *)uuidForDevice; //or #import "UIDevice+FCUUID.h" [[UIDevice currentDevice] uuid];
获取用户设备的 UUID 列表
//returns the list of all uuidForDevice of the same user, in this way it's possible manage guest accounts across multiple devices easily +(NSArray *)uuidsOfUserDevices;
从先前存储的 UUID/UDID 迁移在迁移现有值之前,建议通过简单地传递 commitMigration:NO 并记录返回值来对其进行调试。当您准备好提交迁移时,请使用 commitMigration:YES。 迁移后,将来对 uuidForDevice 的任何调用都将返回迁移后的值。
//these methods search for an existing UUID / UDID stored in the KeyChain or in UserDefaults for the given key / service / access-group +(NSString *)uuidForDeviceMigratingValue:(NSString *)value commitMigration:(BOOL)commitMigration; +(NSString *)uuidForDeviceMigratingValueForKey:(NSString *)key commitMigration:(BOOL)commitMigration; +(NSString *)uuidForDeviceMigratingValueForKey:(NSString *)key service:(NSString *)service commitMigration:(BOOL)commitMigration; +(NSString *)uuidForDeviceMigratingValueForKey:(NSString *)key service:(NSString *)service accessGroup:(NSString *)accessGroup commitMigration:(BOOL)commitMigration;
检查值是否为有效的 UUID
+(BOOL)uuidValueIsValid:(NSString *)uuidValue;
持久化(Persistence)
√
yes-
no*
read notes below
PERSISTS | App memory | App relaunch | Reset Advertising Identifier | App reinstall | System reboot | System upgrade | System reset |
---|---|---|---|---|---|---|---|
uuid
|
- | - | - | - | - | - | - |
uuidForKey:key
|
√ | - | - | - | - | - | - |
uuidForSession
|
√ | - | - | - | - | - | - |
uuidForInstallation
|
√ | √ | √ | - | √ | - | - |
uuidForVendor
|
√ | √ | - | √* | √ | - | - |
uuidForDevice
|
√ | √ | √ | √ | √ | √ | √** |
*(仅在用户尚未卸载同一供应商的所有应用程序时持续存在)
**(仅当用户恢复还包括钥匙串数据的设备备份时才持久)
常问问题
如何在两个应用程序之间共享设备 uuid?
您必须启用 KeyChain 共享(权利和配置配置文件),并且您的应用程序标识符必须具有相同的捆绑包种子。
如果我使用相同的 iCloud 帐户和 iCloud 密钥在2个不同的设备上调用uuidForDevice,会发生什么情况?
您将获得2个不同的 uuid,并且如果调用 uuidsOfUserDevices,则将获得一个包含两个设备的 uuid 的列表。
重新启动/升级/重置设备系统后,设备的 uuid 会更改吗?
请检查上面的持久性表。
支持开发
许可
根据 MIT 许可证发布。
(The first version translated by vz on 2020.07.19)