KeychainItemwrapper生成设备唯一标识

来源:互联网 发布:宏源证券交易软件 编辑:程序博客网 时间:2024/05/22 14:55


1.下载苹果官方文档的项目:https://developer.apple.com/library/ios/samplecode/generickeychain/Introduction/Intro.html
2.把工程文件keychainItemWrapper.h/m两个件添加进自己的工程;
3.添加security.framework到工程;
4.测试代码:

+(NSString *) getKeychainIdentifier{    KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"deviceIdentifier" accessGroup:nil];//    NSString *username = [wrapper objectForKey:(id)kSecAttrAccount];//    NSString *password = [wrapper objectForKey:(id)kSecValueData];    NSString *uniqueIdentifier = [wrapper objectForKey:(id)kSecAttrAccount];    // initially all these are empty//    NSLog(@"username - %@", username); // username -//    NSLog(@"password - %@", password); // password -    NSLog(@"device_identifier:%@",uniqueIdentifier);    if ([uniqueIdentifier isEqualToString:@""]) {        [wrapper setObject:getuuid() forKey:(id)kSecAttrAccount];        NSLog(@"set uniqueIdentifier.");    }        uniqueIdentifier = [wrapper objectForKey:(id)kSecAttrAccount];    NSLog(@"uniqueIdentifier:%@", uniqueIdentifier);    [wrapper release];    return uniqueIdentifier;      //if empty set your credentials//    if ( [username isEqualToString:@""] ) {//        [wrapper setObject:getuuid() forKey:(id)kSecAttrAccount];//    }//    if ( [password isEqualToString:@""] ) {//        [wrapper setObject:getuuid() forKey:(id)kSecValueData];//    }        //get the latest credentials - now you have the set values//    username = [wrapper objectForKey:(id)kSecAttrAccount];//    password = [wrapper objectForKey:(id)kSecValueData];    //    NSLog(@"username - %@", username); // username - your username here//    NSLog(@"password - %@", password); // password - your password here        // reset your keychain items - if  needed    //[wrapper resetKeychainItem];            } //getDeviceIdentifier//get uuid c style functionNSString * getuuid(){    CFUUIDRef uuid_ref = CFUUIDCreate(NULL);    CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);    CFRelease(uuid_ref);    NSString *uuid = [NSString stringWithString:(NSString*)uuid_string_ref];    CFRelease(uuid_string_ref);    return uuid;

注:raywendlich网站关于keychain的接口封装与实现;
http://www.raywenderlich.com/6475/basic-security-in-ios-5-tutorial-part-1

0 0
原创粉丝点击