美文网首页
获取唯一标识符即使app 删除重新装后一样

获取唯一标识符即使app 删除重新装后一样

作者: 泥孩儿0107 | 来源:发表于2018-08-28 15:02 被阅读0次

    @interfaceAppDelegate ()

    +(NSString *)getDeviceIDInKeychain;

    @end

    NSString *constKEY_UDID_INSTEAD =@"com.myapp.udid.test";

    @implementation AppDelegate

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

        idret =nil;

        NSMutableDictionary *keychainQuery = [selfgetKeychainQuery: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:(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:(__bridgeNSData *)keyData];

            }@catch(NSException *e) {

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

            }@finally{

            }

        }

        if(keyData)

            CFRelease(keyData);

        returnret;

    }

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

        NSMutableDictionary *keychainQuery = [selfgetKeychainQuery:service];

        SecItemDelete((CFDictionaryRef)keychainQuery);

    }

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

        //Get search dictionary

        NSMutableDictionary *keychainQuery = [selfgetKeychainQuery:service];

        //Delete old item before add new item

        SecItemDelete((CFDictionaryRef)keychainQuery);

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

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

        //Add item to keychain with the search dictionary

        SecItemAdd((CFDictionaryRef)keychainQuery,NULL);

    }

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

        return[NSMutableDictionary dictionaryWithObjectsAndKeys:

                (id)kSecClassGenericPassword,(id)kSecClass,

                service, (id)kSecAttrService,

                service, (id)kSecAttrAccount,

                (id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,

                nil];

    }

    +(NSString *)getDeviceIDInKeychain {

        NSString *getUDIDInKeychain = (NSString *)[AppDelegate load:KEY_UDID_INSTEAD];

        NSLog(@"从keychain中获取到的 UDID_INSTEAD %@",getUDIDInKeychain);

        if(!getUDIDInKeychain ||[getUDIDInKeychain isEqualToString:@""]||[getUDIDInKeychain isKindOfClass:[NSNull class]]) {

            CFUUIDRef puuid = CFUUIDCreate(nil);

            CFStringRef uuidString = CFUUIDCreateString(nil, puuid );

            NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy(NULL, uuidString));

            CFRelease(puuid);

            CFRelease(uuidString);

            NSLog(@"\n \n \n _____重新存储 UUID _____\n \n \n  %@",result);

            [AppDelegate save:KEY_UDID_INSTEAD data:result];

            getUDIDInKeychain = (NSString *)[AppDelegate load:KEY_UDID_INSTEAD];

        }

        NSLog(@"最终 ———— UDID_INSTEAD %@",getUDIDInKeychain);

        returngetUDIDInKeychain;

    }

    相关文章

      网友评论

          本文标题:获取唯一标识符即使app 删除重新装后一样

          本文链接:https://www.haomeiwen.com/subject/uxzziftx.html