IPhone UDID替代方案

具体实现来自网上,我只是作了些简单的修改;

 

/**
 *  手机UUID结构体
 */
typedef struct
{
    UInt8 byte0;
    UInt8 byte1;
    UInt8 byte2;
    UInt8 byte3;
    
}PhoneUUID;

 

-(PhoneUUID)CBLUUID
{
    NSString *uuid;

    NSString *bundleName   = [[NSBundle mainBundle] infoDictionary][@"CFBundleIdentifier"];
    
    NSString * const KEY_USERNAME_PASSWORD = [NSString stringWithFormat:@"userName.%@",bundleName]; // 存取密码字典的用户名key
    NSString * const KEY_PASSWORD = [NSString stringWithFormat:@"passWord.%@",bundleName]; // 获取字典中UUID的key值
    NSMutableDictionary *readUserPwd = (NSMutableDictionary *)[self load:KEY_USERNAME_PASSWORD];
    
    if (readUserPwd) {
        
        uuid = readUserPwd[KEY_PASSWORD];
        
    }else
    {
        NSString *identifierStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        
        NSLog(@"identifierStr = %@",identifierStr);
        
        NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionary];
        
        [usernamepasswordKVPairs setObject:identifierStr forKey:KEY_PASSWORD];
        
        //存
        [self save:KEY_USERNAME_PASSWORD data:usernamepasswordKVPairs];
        
        uuid = identifierStr;
    }
    
    PhoneUUID tempPhoneUUID;
    
    if (uuid) {
        
        NSUUID *tempUUID = [[NSUUID alloc] initWithUUIDString:uuid];
        
        // 获取4字节的UUID
        uuid_t uuidt;
        [tempUUID getUUIDBytes:uuidt];
        
        tempPhoneUUID.byte0 = uuidt[12];
        tempPhoneUUID.byte1 = uuidt[13];
        tempPhoneUUID.byte2 = uuidt[14];
        tempPhoneUUID.byte3 = uuidt[15];
    }
    
    
    return tempPhoneUUID;
}

 

// 存入UUID
- (void)save:(NSString *)service data:(id)data {
    //Get search dictionary
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    //Delete old item before add new item
    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
    //Add new object to search dictionary(Attention:the data format)
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData];
    //Add item to keychain with the search dictionary
    SecItemAdd((__bridge CFDictionaryRef)keychainQuery, NULL);
    
    
}

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

// 取出UUID
- (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:(__bridge id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
    [keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
    CFDataRef keyData = NULL;
    if (SecItemCopyMatching((__bridge 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;
}

// 清楚缓存的UUID
- (void)delete:(NSString *)service {
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
}

 

posted on 2014-12-11 16:30  超级庄子  阅读(272)  评论(0编辑  收藏  举报

导航