前言
UUID是什么?UUID其实我们可以理解为应用程序唯一的编号,通俗的可以理解为人的身份证号码一样,是独一无二的。在IOS4.0之前,UUID是对开发者开放的,但是之后为了保护用户的隐私,苹果公司就把这个UUID给禁掉了,但是我们开发人员有时候还是需要这个东东的,所以我们找了一种替代的方法。本文就如何生存替代的 UUID 和 UUID 存放在不同的位置作解释。
<span style="font-size:18px;">#import <Foundation/Foundation.h> @interface GetDeviceUUID : NSObject /** * 随机生成一个UUID * * @param prefix 生成UUID固定的前缀 * * @return 返回一个随机的UUID字符串 */ + (NSString *)createCUID:(NSString *)prefix; /** * 随机生成一个UUID并保存在 keychain 中 * * @param prefix 生成UUID固定的前缀 * * @return 返回一个随机的UUID字符串 */ + (NSString *)createUUIDInKeychain:(NSString *)prefix; /** * 随机生成一个UUID并保存在 keychain 中 * * @param prefix 生成UUID固定的前缀 * * @return 返回一个随机的UUID字符串 */ + (NSString *)createUUIDInNSUserDefault:(NSString *)prefix; @end </span>
<span style="font-size:18px;">#import "GetDeviceUUID.h" #import "KeychainItemWrapper.h" @implementation GetDeviceUUID //随机生成一个UUID + (NSString *)createCUID:(NSString *)prefix { NSString * result; CFUUIDRef uuid; CFStringRef uuidStr; uuid = CFUUIDCreate(NULL); uuidStr = CFUUIDCreateString(NULL, uuid); result =[NSString stringWithFormat:@"%@-%@", prefix,uuidStr]; CFRelease(uuidStr); CFRelease(uuid); return result; } //随机生成一个UUID并保存在 keychain 中 + (NSString *)createUUIDInKeychain:(NSString *)prefix { KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"Account Number" accessGroup:nil]; NSString *keychainUUID = [wrapper objectForKey:(__bridge id)kSecAttrAccount]; if (keychainUUID.length >0) { return keychainUUID; } NSString * result; CFUUIDRef uuid; CFStringRef uuidStr; uuid = CFUUIDCreate(NULL); uuidStr = CFUUIDCreateString(NULL, uuid); result =[NSString stringWithFormat:@"%@-%@", prefix,uuidStr]; CFRelease(uuidStr); CFRelease(uuid); //将生成的UUID保存在 keychain 中 [wrapper setObject:result forKey:(__bridge id)kSecAttrAccount]; return result; } //随机生成一个UUID并保存在 NSUserDefaults 中 + (NSString *)createUUIDInNSUserDefault:(NSString *)prefix { NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; NSString *defaultUUID = [userDefaults stringForKey:@"UUID"]; if (defaultUUID.length >0) { return defaultUUID; } NSString * result; CFUUIDRef uuid; CFStringRef uuidStr; uuid = CFUUIDCreate(NULL); uuidStr = CFUUIDCreateString(NULL, uuid); result =[NSString stringWithFormat:@"%@-%@", prefix,uuidStr]; CFRelease(uuidStr); CFRelease(uuid); [userDefaults setObject:result forKey:@"UUID"]; [userDefaults synchronize]; return result; } @end </span>
以下是调用测试的代码片段:
#import "ViewController.h" #import "KeychainItemWrapper.h" #import "GetDeviceUUID.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(5, 60, self.view.frame.size.width-10, 25)]; label1.text = @"测试随机生成UUID,随机可变"; label1.font = [UIFont systemFontOfSize:15.0]; label1.textColor = [UIColor grayColor]; [self.view addSubview:label1]; UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(label1.frame), self.view.frame.size.width-20, 40)]; label2.lineBreakMode = NSLineBreakByCharWrapping; label2.numberOfLines = 0; label2.text = [GetDeviceUUID createCUID:@"Huoshan"]; label2.font = [UIFont systemFontOfSize:15.0]; label2.textColor = [UIColor redColor]; [self.view addSubview:label2]; UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(label2.frame)+20, self.view.frame.size.width-10, 25)]; label3.text = @"测试生成UUID保存在 keychain 中,固定不变"; label3.font = [UIFont systemFontOfSize:15.0]; label3.textColor = [UIColor grayColor]; [self.view addSubview:label3]; UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(label3.frame), self.view.frame.size.width-10, 40)]; label4.numberOfLines = 0; label4.text = [GetDeviceUUID createUUIDInKeychain:@"Yuancan"]; label4.font = [UIFont systemFontOfSize:15.0]; label4.textColor = [UIColor redColor]; label4.lineBreakMode = NSLineBreakByCharWrapping; [self.view addSubview:label4]; UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(label4.frame)+20, self.view.frame.size.width-10, 25)]; label5.text = @"测试生成UUID保存在 NSUserDefault 中,删除应用可变"; label5.font = [UIFont systemFontOfSize:15.0]; label5.textColor = [UIColor grayColor]; [self.view addSubview:label5]; UILabel *label6 = [[UILabel alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(label5.frame), self.view.frame.size.width-10, 40)]; label6.numberOfLines = 0; label6.text = [GetDeviceUUID createUUIDInNSUserDefault:@"Robert"]; label6.font = [UIFont systemFontOfSize:15.0]; label6.textColor = [UIColor redColor]; label6.lineBreakMode = NSLineBreakByCharWrapping; [self.view addSubview:label6]; } @end
资源下载地址:http://download.csdn.net/detail/u010545480/8788283