首页 > 其他 > 详细

iOS运用keychain 把identifierForVendor当成唯一标识。替换advertisingIdentifier

时间:2014-02-15 01:28:53      阅读:556      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
iOS运用keychain   结合[[[UIDevice currentDevice] identifierForVendor] UUIDString]
取得设备唯一标识


 NSString *identifierStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString]
    NSString * const KEY_USERNAME_PASSWORD = @"com.snda.app.usernamepassword";
    NSString * const KEY_PASSWORD = @"com.snda.app.password";
    NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionary];
    [usernamepasswordKVPairs setObject:identifierStr forKey:KEY_PASSWORD];

    
    //
    [ViewController save:KEY_USERNAME_PASSWORD data:usernamepasswordKVPairs];
    
    NSMutableDictionary *readUserPwd = (NSMutableDictionary *)[ViewController load:KEY_USERNAME_PASSWORD];
    NSLog(@"%@",readUserPwd);



//
+ (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];
}

//
+ (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;
}


+ (void)delete:(NSString *)service {
    NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
    SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
}
bubuko.com,布布扣

iOS运用keychain 把identifierForVendor当成唯一标识。替换advertisingIdentifier

原文:http://www.cnblogs.com/qingjoin/p/3549325.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!