Get a unique device ID

[[[UIDevice currentDevice] identifierForVendor] UUIDString];
according to the supplier distribution APP, APP after unloading heavy equipment, regenerates.
You need to use the keychain storage complexes.

YLKeyChainStore @interface: the NSObject
/ ** save data to a keychain * /

  • (void) Save: (NSString *) Data-Service: (ID) Data;
    / ** load data from the key string * /
  • (the above mentioned id) the Load: (NSString *) Service;
    / ** * Delete data /
  • (void)deleteKeyData:(NSString*)service;
    /** 获取UUID*/
  • (NSString *)getUUIDByKeyChain;
    @end

/ ** obtain data by key * /

  • (NSMutableDictionary)getKeychainQuery:(NSString)service {
    service = [self handleServiceKeyWith:service];
    return[NSMutableDictionary dictionaryWithObjectsAndKeys:
    (id)kSecClassGenericPassword,(id)kSecClass,
    service,(id)kSecAttrService,
    service,(id)kSecAttrAccount,
    (id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
    nil];
    }

  • (NSString)handleServiceKeyWith:(NSString)service {
    return [NSString stringWithFormat:@"%@%@",AppBundleID(),service];
    }

  • (void)save:(NSString)service data:(id)data{
    //Get search dictionary
    NSMutableDictionary
    keychainQuery = [self getKeychainQuery:service];
    //Delete old item before add new item
    SecItemDelete((CFDictionaryRef)keychainQuery);
    //Add new object to searchdictionary(Attention:the data format)
    [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data]forKey:(id)kSecValueData];
    //Add item to keychain with the searchdictionary
    SecItemAdd((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 areexpecting only a single attribute to be returned (the password) wecan 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(@"Unarchiveof %@ failed: %@",[self handleServiceKeyWith:service], e);
    }@finally{
    }
    }
    if(keyData)
    CFRelease(keyData);
    return ret;
    }

  • (void)deleteKeyData:(NSString)service {
    NSMutableDictionary
    keychainQuery = [self getKeychainQuery:service];
    SecItemDelete((CFDictionaryRef)keychainQuery);
    }

/ ** Gets UUID * /

  • (NSString ) getUUIDByKeyChain {
    NSString
    key = @ "uuid";
    // prefix key is your best BundleID
    NSString strUUID = (NSString ) [YLKeyChainStore the Load: key];
    // the first implementation of the method, uuid is empty
    if ([strUUID as isEqualToString: @ ""] || strUUID!)
    {
    strUUID = [[[currentDevice the UIDevice] identifierForVendor] UUIDString];
    // save the uuid to Keychain
    [YLKeyChainStore save: Key Data: strUUID];
    }
    return strUUID;
    }

Guess you like

Origin www.cnblogs.com/xbios/p/11890522.html