iOS取得唯一设备标识
//
// DeviceUID.h
//
#import <Foundation/Foundation.h>
@interface DeviceUID : NSObject
@property (nonatomic, strong) NSString *uid;
@property (nonatomic, strong) NSString *uidKey;
+(instancetype)shareDeviceUID;
@end
//
// DeviceUID.m
//
#import "DeviceUID.h"
#define DEFAULT_UID_KEY @"com.agate.agate";
@implementation DeviceUID
+(instancetype)shareDeviceUID
{
static DeviceUID *deviceUID = nil;
static dispatch_once_t device_once;
dispatch_once(&device_once, ^{
deviceUID = [[DeviceUID alloc] init];
});
return deviceUID;
}
-(id)init
{
self = [super init];
if (self) {
_uidKey = DEFAULT_UID_KEY;
_uid = [self getValue:(__bridge_transfer id)kSecValueData inService:_uidKey];
}
return self;
}
- (NSMutableDictionary *)getKeychainQuery:(NSString *)service {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
(__bridge_transfer id)kSecClassGenericPassword,(__bridge_transfer id)kSecClass,
service, (__bridge_transfer id)kSecAttrService,
service, (__bridge_transfer id)kSecAttrAccount,
(__bridge_transfer id)kSecAttrAccessibleAfterFirstUnlock,(__bridge_transfer id)kSecAttrAccessible,
nil];
}
-(NSString *)getValue:(NSString *)key inService:(NSString *)service{
id ret = nil;
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
//Configure the search setting
[keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge_transfer id)kSecReturnData];
[keychainQuery setObject:(__bridge_transfer id)kSecMatchLimitOne forKey:(__bridge_transfer id)kSecMatchLimit];
CFDataRef keyData = NULL;
if (SecItemCopyMatching((__bridge_retained CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge_transfer NSData *)keyData];
} @catch (NSException *e) {
NSLog(@"Unarchive of %@ failed: %@", service, e);
} @finally {
}
}
return ret;
}
-(void)setValue:(NSString *)value forKey:(NSString *)key inService:(NSString *)service {
NSMutableDictionary *keychainItem = [[NSMutableDictionary alloc] init];
keychainItem[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
keychainItem[(__bridge id)kSecAttrAccessible] = (__bridge id)kSecAttrAccessibleAlways;
keychainItem[(__bridge id)kSecAttrAccount] = key;
keychainItem[(__bridge id)kSecAttrService] = service;
keychainItem[(__bridge id)kSecValueData] = [value dataUsingEncoding:NSUTF8StringEncoding];
SecItemAdd((__bridge CFDictionaryRef)keychainItem, NULL);
}
- (NSString *)uid {
if (_uid == nil) {
_uid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
[self save:_uidKey data:_uid];
}
return _uid;
}
- (void)save:(NSString *)service data:(id)data {
//Get search dictionary
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
//Delete old item before add new item
SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery);
//Add new object to search dictionary(Attention:the data format)
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge_transfer id)kSecValueData];
//Add item to keychain with the search dictionary
SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, NULL);
}
@end