码迷,mamicode.com
首页 > 其他好文 > 详细

获取唯一设备ID

时间:2019-11-19 17:20:49      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:expec   release   turned   object   ssg   handle   elf   通过   attrs   

[[[UIDevice currentDevice] identifierForVendor] UUIDString];
根据APP供应商分配,APP卸载重装后,会重新生成。
需要通过keychain存储配合使用。

@interface YLKeyChainStore : NSObject
/**保存数据至钥匙串中*/

  • (void)save:(NSString*)service data:(id)data;
    /**从钥匙串中加载数据*/
  • (id)load:(NSString*)service;
    /**删除数据*/
  • (void)deleteKeyData:(NSString*)service;
    /** 获取UUID*/
  • (NSString *)getUUIDByKeyChain;
    @end

/**通过 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);
    }

/** 获取UUID*/

  • (NSString )getUUIDByKeyChain {
    NSString
    key = @"uuid";
    // 这个key的前缀最好是你的BundleID
    NSStringstrUUID = (NSString)[YLKeyChainStore load:key];
    //首次执行该方法时,uuid为空
    if([strUUID isEqualToString:@""] || !strUUID)
    {
    strUUID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    //将该uuid保存到keychain
    [YLKeyChainStore save:key data:strUUID];
    }
    return strUUID;
    }

获取唯一设备ID

标签:expec   release   turned   object   ssg   handle   elf   通过   attrs   

原文地址:https://www.cnblogs.com/xbios/p/11890522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!