博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 设备唯一UDID获取
阅读量:5996 次
发布时间:2019-06-20

本文共 2877 字,大约阅读时间需要 9 分钟。

使用的方法以及封装的工具类, 在应用里使用使用keyChain,我们需要导入Security.framework。创建一个获取唯一标识的工具类:

XMGKeychain.h 文件中就这一个方法

@interface XMGKeychain : NSObject  +(NSString *)getDeviceIDInKeychain;  @end复制代码

XMGKeychain.m 文件中如下

NSString * const KEY_UDID_INSTEAD = @"com.myapp.udid.test";@implementation XMGKeychain+(NSString *)getDeviceIDInKeychain{NSString *getUDIDInKeychain = (NSString *)[self load:KEY_UDID_INSTEAD];NSLog(@"从keychain中获取到的 UDID_INSTEAD %@",getUDIDInKeychain);if (!getUDIDInKeychain ||[getUDIDInKeychain isEqualToString:@""]||[getUDIDInKeychain isKindOfClass:[NSNull class]]) {    CFUUIDRef puuid = CFUUIDCreate( nil );    CFStringRef uuidString = CFUUIDCreateString( nil, puuid );    NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));    CFRelease(puuid);    CFRelease(uuidString);    NSLog(@"\n \n \n _____重新存储 UUID _____\n \n \n  %@",result);    [self save:KEY_UDID_INSTEAD data:result];    getUDIDInKeychain = (NSString *)[self load:KEY_UDID_INSTEAD];}NSLog(@"最终 ———— UDID_INSTEAD %@",getUDIDInKeychain);return getUDIDInKeychain;}#pragma mark - private  + (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 dictionaryNSMutableDictionary *keychainQuery = [self getKeychainQuery:service];//Delete old item before add new itemSecItemDelete((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 dictionarySecItemAdd((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)delete:(NSString *)service {NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];SecItemDelete((CFDictionaryRef)keychainQuery);}@end复制代码

转载地址:http://qeqlx.baihongyu.com/

你可能感兴趣的文章
浅谈oracle中for update 和 for update nowait 和 for update wait x的区别
查看>>
DOS与内存
查看>>
[Micropython]发光二极管制作炫彩跑马灯
查看>>
jQuery获取Select选择的Text和 Value(转)
查看>>
C# System.IO.Path
查看>>
[基准测试]----lmbench
查看>>
Core源码(一) ConcurrentDictionary
查看>>
Visual Studio Code管理MySQL
查看>>
Mysql锁机制--写锁
查看>>
VO、DTO、DO、PO
查看>>
Java知多少(51)finally
查看>>
【翻译】理解内存
查看>>
HNUSTOJ-1258 Time
查看>>
bzoj1061 1283
查看>>
oscached
查看>>
L119
查看>>
两列自适应布局的4种思路
查看>>
UITextField 限制输入金额(项目中遇到判断输入金额)
查看>>
openresty
查看>>
抽象类特点
查看>>