大致原理:通过将首次安装生成的UUID保存到keychain中,以后每次生成都先读取keychain里面的UUID
当然,同一个开发者账号用同一个key来保存,那么该账号下面所有的应用都能获取到同一个UUID
1.Prefix.pch 定义宏
#define KEY_USERNAME_PASSWORD @"com.company.yourapp.usernamepassword"
#define KEY_USERNAME @"com.company.yourapp.username"
#define KEY_PASSWORD @"com.company.yourapp.password"
注意 Prefix.pch文件项目中没有需要自己生成,然后去TARGETS -> PROJECT(你的项目名) -> Build Setting下面搜索 ""Prefix Header ,然后修改Precompile Prefix Header 选项为Yes
然后修改路径 PrefixHeader (xxx.xcodeproj目录下面的xxx.pch)
es:我的Prefix.pch放在 xxx.xcodeproj下面的ios文件夹下面,则路径为 ios/Prefix.pch
2.keyChainStore.h
//
// keyChainStore.h
//
// Created by wacelike on 2020/9/2.
//
#ifndef keyChainStore_h
#define keyChainStore_h
#import <Foundation/Foundation.h>
@interface KeyChainStore : NSObject
+ (void)save:(NSString *)service data:(id)data;
+ (id)load:(NSString *)service;
+ (void)deleteKeyData:(NSString *)service;
@end
#endif /* keyChainStore_h */
3.KeyChainStore.m
//
// keyChainStore.m
//
// Created by wacelike on 2020/9/2.
//
#import "KeyChainStore.h"
@implementation KeyChainStore
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
(id)kSecClassGenericPassword,(id)kSecClass,
service, (id)kSecAttrService,
service, (id)kSecAttrAccount,
(id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
nil];
}
+ (void)save:(NSString *)service data:(id)data {
//Get search dictionary
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
//Delete old item before add new item
SecItemDelete((CFDictionaryRef)keychainQuery);
//Add new object to search dictionary(Attention:the data format)
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
//Add item to keychain with the search dictionary
SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}
+ (id)load:(NSString *)service {
id ret = nil;
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
//Configure the search setting
//Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue
[keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
[keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
CFDataRef keyData = NULL;
if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
} @catch (NSException *e) {
NSLog(@"Unarchive of %@ failed: %@", service, e);
} @finally {
}
}
if (keyData)
CFRelease(keyData);
return ret;
}
+ (void)deleteKeyData:(NSString *)service {
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
}
@end
以上两个文件夹我放在了uuid文件夹下面,下面为调用方法:
调用位置在我的Utils.m中
#import "keyChainStore.h"
@implementation Utils
+(NSString*) getUUID
{
NSString * strUUID = (NSString *)[KeyChainStore load:@"com.heart.testAppLogin.usernamepassword"];
NSLog(@"保存的strUUID = %@",strUUID);
//首次执行该方法时,uuid为空
if ([strUUID isEqualToString:@""] || !strUUID)
{
//生成一个uuid的方法
CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));
//将该uuid保存到keychain
[KeyChainStore save:KEY_USERNAME_PASSWORD data:strUUID];
}
NSLog(@"getUUID str = %@",strUUID);
return strUUID;
}
@end
别忘记引入头文件#import "keyChainStore.h"
好了,我们可以在AppController.m中调用获取uuid的方法了
NSString* uuid = [Utils getUUID];
NSLog(@"uuid:%@",uuid);
最后,我们来测试一下,首次安装打印保存的uuid为null,然后生成了一个uuid为 558747E1-6F7E-465C-B5E0-90CCAD696D5B,然后卸载应用,重新运行,打印保存的uuid为 558747E1-6F7E-465C-B5E0-90CCAD696D5B。两次获取的uuid没有改变了,完成。