获取UUID的方法有很多,但是都会因为各种原因改变
/** 卸载应用重新安装后会不一致*/
+ (NSString *)getUUID{
CFUUIDRef uuid = CFUUIDCreate(NULL);
NSString *UUID = (__bridge_transfer NSString *)CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
return UUID;
}
/** 卸载应用重新安装后会不一致*/
+ (NSString *)getUUID{
return [UIDevice currentDevice].identifierForVendor.UUIDString;;
}
/** 不会因为应用卸载改变
* 但是用户在设置-隐私-广告里面限制广告跟踪后会变成@"00000000-0000-0000-0000-000000000000"
* 重新打开后会变成另一个,还原广告标识符也会变
*/
+ (NSString *)getUUID{
return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
}
作为设备唯一的标识符,一言不合就变了肯定是不行的呀
在产品汪的逼迫下,在我不懈的努力下,我终于找到一个可以一直保持一致的方法。第一次获取成功后保存到钥匙串,然后每次使用都通过钥匙串去拿。
第一步,打开应用的钥匙串权限
Target - Capabilities - KeychainSharing - ON
第二步,复制粘贴代码,懒得复制的可以去我的Github上直接下载
先粘.h文件
#import <Foundation/Foundation.h>
@interface KeyChainStore : NSObject
+ (void)save:(NSString*)service data:(id)data;
+ (id)load:(NSString*)service;
+ (void)deleteKeyData:(NSString*)service;
@end
再粘.m文件
#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 searchdictionary(Attention:the data format)
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data]forKey:(id)kSecValueData];
//Add item to keychain with the searchdictionary
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 areexpecting only a single attribute to be returned (the password) wecan 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(@"Unarchiveof %@ failed: %@",service, e);
}@finally{
}
}
if(keyData)
CFRelease(keyData);
return ret;
}
+ (void)deleteKeyData:(NSString*)service {
NSMutableDictionary*keychainQuery = [self getKeychainQuery:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
}
@end
第三步,使用(直接调这个方法就可以了)
/** 获取UUID*/
+ (NSString *)getUUIDByKeyChain{
// 这个key的前缀最好是你的BundleID
NSString*strUUID = (NSString*)[KeyChainStore load:@"com.mycompany.myapp.usernamepassword"];
//首次执行该方法时,uuid为空
if([strUUID isEqualToString:@""]|| !strUUID)
{
// 获取UUID 这个是要引入<AdSupport/AdSupport.h>的
strUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
if(strUUID.length ==0 || [strUUID isEqualToString:@"00000000-0000-0000-0000-000000000000"])
{
//生成一个uuid的方法
CFUUIDRef uuidRef= CFUUIDCreate(kCFAllocatorDefault);
strUUID = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault,uuidRef));
CFRelease(uuidRef);
}
//将该uuid保存到keychain
[KeyChainStore save:@"com.mycompany.myapp.usernamepassword" data:strUUID];
}
return strUUID;
}
无论你怎么折腾都会保证同一设备每次获取到的UUID都是一致的,卸载应用,开启广告限制跟踪,系统升级都不会有影响。越狱刷机这种骚操作我没有测过,即使有问题,感觉这些调皮的用户也可以忽略了,因为这个已经是目前最好的解决办法了,如果大家有什么更好的解决方案,还请评论区指正。
参考资料:https://blog.csdn.net/wsdxsYB/article/details/51773494