Ios 获取手机不变的UDID

感谢大神的解惑        https://www.cnblogs.com/smileEvday/p/UDID.html


导入头文件
#import  <AdSupport/AdSupport.h>

#import "KeyChainStore.h"


废话不多说,直接上代码,

+(NSString *)CkeckUUID {

    // 这个key的前缀最好是你的BundleID

    NSString*strUUID = (NSString*)[KeyChainStoreload:@"com.skrApp"];

    //首次执行该方法时,uuid为空

    if([strUUIDisEqualToString:@""]|| !strUUID)

    {

        // 获取UUID 这个是要引入的

        strUUID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];


        if(strUUID.length ==0 || [strUUID isEqualToString:@"00000000-0000-0000-0000-000000000000"])

        {

            //生成一个uuid的方法

            CFUUIDRef uuidRef= CFUUIDCreate(kCFAllocatorDefault);

            strUUID = (NSString*)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault,uuidRef));

            CFRelease(uuidRef);

        }

        //将该uuid保存到keychain

        [KeyChainStoresave:@"com.skrApp"data:strUUID];

    }

    returnstrUUID;

}


KeyChainStore.h 文件

#import

NS_ASSUME_NONNULL_BEGIN

@interface KeyChainStore : NSObject

+ (void)save:(NSString*)servicedata:(id)data;

+ (id)load:(NSString*)service;

+ (void)deleteKeyData:(NSString*)service;

@end

NS_ASSUME_NONNULL_END



KeyChainStore.m 文件

#import "KeyChainStore.h"

@implementation KeyChainStore

+ (NSMutableDictionary*)getKeychainQuery:(NSString*)service {

    return[NSMutableDictionary dictionaryWithObjectsAndKeys:

           (id)kSecClassGenericPassword,(id)kSecClass,

           service,(id)kSecAttrService,

           service,(id)kSecAttrAccount,

           (id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,

           nil];

}

+ (void)save:(NSString*)servicedata:(id)data{

    //Get search dictionary

    NSMutableDictionary*keychainQuery = [selfgetKeychainQuery:service];

    //Delete old item before add new item

    SecItemDelete((CFDictionaryRef)keychainQuery);

    //Add new object to searchdictionary(Attention:the data format)

    [keychainQuerysetObject:[NSKeyedArchiver archivedDataWithRootObject:data]forKey:(id)kSecValueData];

    //Add item to keychain with the searchdictionary

    SecItemAdd((CFDictionaryRef)keychainQuery,NULL);

}

+ (id)load:(NSString*)service {

    idret =nil;

    NSMutableDictionary*keychainQuery = [selfgetKeychainQuery: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

    [keychainQuerysetObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];

    [keychainQuerysetObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];

    CFDataRefkeyData =NULL;

    if(SecItemCopyMatching((CFDictionaryRef)keychainQuery,(CFTypeRef*)&keyData) ==noErr){

        @try{

            ret =[NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData*)keyData];

        }@catch(NSException *e) {

            NSLog(@"Unarchiveof %@ failed: %@",service, e);

        }@finally{

        }

    }

    if(keyData)

        CFRelease(keyData);

    returnret;

}

+ (void)deleteKeyData:(NSString*)service {

    NSMutableDictionary*keychainQuery = [selfgetKeychainQuery:service];

    SecItemDelete((CFDictionaryRef)keychainQuery);

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。