iOS如何获取设备的唯一标识(UUID)

前段时间后台做统计功能的时候需要用到设备的唯一标识,所以得客户端这边获取设备的唯一标识并将其上传到服务器。对于iOS,说到设备的唯一标识,估计大家都会不由自主的想到UUID。那该如何去获取设备的UUID呢?其实很简单,一行代码即可获取
[[UIDevice currentDevice] identifierForVendor] UUIDString] .

但是这样的话存在一定的弊端,当你升级或者重装软件的时候,UUID会发生变化,这时候我们再去获取UUID时,得到的值和以前的不一样,但其实还是同一台设备,这样的话会造成我们的统计数据不准。所以说我们在获取到UUID后需要解决的是如何保证它的唯一性。

keychain顾名思义,是钥匙串的意思,是苹果公司Mac OS中的密码管理系统。keychain里保存的信息不会因App被删除而丢失,在用户重新安装App后依然有效,数据还在。所以我们可以将UUID存储到keychain里面,需要用到的时候从keychain中取,这样可以保证UUID的唯一性。

首先,我们新建一个类AppKeyChain,管理keychain.
AppKeyChain.h

@interface AppKeyChain : NSObject

+ (void)saveData:(id)data forKey:(NSString *)key;//将数据写入keychain中

+ (id)loadForKey:(NSString *)key;//从keychain中获取数据

+ (void)deleteKeyData:(NSString *)key;//删除keychain中的相应数据

@end

AppKeyChain.m中实现相应的方法

 #import "AppKeyChain.h"
#import  
 

@implementation AppKeyChain

#pragma mark - 保存和读取UUID

+ (NSMutableDictionary *)getKeychainQuery:(NSString *)key {
    
    
    return [NSMutableDictionary dictionaryWithObjectsAndKeys:
            (id)kSecClassGenericPassword,(id)kSecClass,
            key, (id)kSecAttrService,
            key, (id)kSecAttrAccount,
            (id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible, nil];
}

+ (void)saveData:(id)data forKey:(NSString *)key {
    //获得keychain字典
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:key];
    
    //删除旧值
    SecItemDelete((CFDictionaryRef)keychainQuery);
    
    //添加新值
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
    
//    将新的信息添加到keychain中
    SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}

+ (id)loadForKey:(NSString *)key {
    
    id ret = nil;
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:key];
    
    //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(@"存储失败,key-- %@  exception-- %@", key, e);
        } @finally {
        }
    }

    if (keyData) {
        CFRelease(keyData);
    }
    
    return ret;
}

+ (void)deleteKeyData:(NSString *)key {
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:key];
    SecItemDelete((CFDictionaryRef)keychainQuery);
}

@end

然后,再创建一个管理UUID的类UUIDManager

在` UUIDManager.h`中声明一个获取设备UUID的方法
#import 

@interface UUIDManager : NSObject
// 获取设备唯一标识
+ (NSString *)getDeviceID;
@end

UUIDManager.m中实现对应的方法

#import "UUIDManager.h"
#import "AppKeyChain.h"
@implementation UUIDManager
+ (NSString *)getDeviceID {
    // 读取keyChain存储的UUID
    NSString * strUUID = (NSString *)[AppKeyChain loadForKey: @"uuid"];
    // 首次运行生成一个UUID并用keyChain存储
    if ([strUUID isEqualToString: @""] || !strUUID) {
        // 生成uuid
        CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
        strUUID = (NSString *)CFBridgingRelease(CFUUIDCreateString (kCFAllocatorDefault,uuidRef));
        // 将该uuid用keychain存储
        [AppKeyChain saveData: strUUID forKey: @"uuid"];
    }
    return strUUID;
}
@end

最后,将UUIDManager导入到自己的工程中。
AppDelegate.m将UUID存储下来

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
//存储设备的UUID
  [UUIDManager getDeviceID];
    
    return YES;
}

这样,设备的UUID已经被存储到keychain中,这时候你就可以随时随地获取设备的唯一标识UUID了

比如我在ViewController.m中取用UUID,直接进入UUIDManager

 
- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *deviceUUID  = [UUIDManager getDeviceID];
 
    NSLog(@"获取设备的UUID:%@",deviceUUID);
 
    
}

demo地址

你可能感兴趣的:(iOS如何获取设备的唯一标识(UUID))