Ios 14 version directly obtain IDFA, IDFV, UUID and other unreliable.

The obtained IDFA, IDFV, or generated UUID is stored in the keychain.

Get the UUID code:

obf_DeviceUUID_fuc.h

#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface obf_DeviceUUID_fuc : NSObject// getIDFA + (NSString *)getIDFA; // getIDFV + (NSString *)getIDFV; // getUUID //+ (NSString *)getUUID; // getUQID + (NSString *)getUQID; @endCopy the code

obf_DeviceUUID_fuc.m

#import "obf_DeviceUUID_fuc.h" #import "obf_KeyChain_fuc.h" #import <AdSupport/ASIdentifierManager.h> #define UQID_KEY Device.uqid "@implementation obf_DeviceUUID_fuc getIDFA {return [[ASIdentifierManager  sharedManager] advertisingIdentifier] UUIDString]; } // getIDFV + (NSString *)getIDFV {return [[UIDevice currentDevice]. IdentifierForVendor UUIDString]; } // getUUID + (NSString *)getUUID {return [[NSUUID UUID] UUIDString]; } // getUQID (NSString *)getUQID {// get NSString * UQID = [[NSUserDefaults standardUserDefaults] objectForKey:UQID_KEY]; if (! Uqid) {// Get uqID from keychain = (NSString *)[obf_KeyChain_fuc readObjectForKey:UQID_KEY]; if (uqid) { [[NSUserDefaults standardUserDefaults] setObject:uqid forKey:UQID_KEY]; [[NSUserDefaults standardUserDefaults] synchronize]; } else {// pasteboard *pasteboard = [UIPasteboard generalPasteboard]; // id data = [pasteboard dataForPasteboardType:UQID_KEY]; // if (data) { // uqid = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // } // if (uqid) { // [[NSUserDefaults standardUserDefaults] setObject:uqid forKey:UQID_KEY]; // [[NSUserDefaults standardUserDefaults] synchronize]; // [obf_KeyChain_fuc saveObject:uqid forKey:UQID_KEY]; } else {// getIDFA uqid = [self getIDFA]; // Get idFV if (! uqid || [uqid isEqualToString:@"00000000-0000-0000-0000-000000000000"]) { uqid = [self getIDFV]; // if the idFv fails to obtain the uuid if (! uqid) { uqid = [self getUUID]; } // } [[NSUserDefaults standardUserDefaults] setObject:uqid forKey:UQID_KEY]; [[NSUserDefaults standardUserDefaults] synchronize]; [obf_KeyChain_fuc saveObject:uqid forKey:UQID_KEY]; UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; NSData *data = [uqid dataUsingEncoding:NSUTF8StringEncoding]; [pasteboard setData:data forPasteboardType:UQID_KEY]; } } } return uqid; } @endCopy the code

Operation keychain code:

obf_KeyChain_fuc.h

#import <Foundation/Foundation.h>
@interface obf_KeyChain_fuc : NSObject
+ (void)saveObject:(id)object forKey:(NSString *)key;
+ (id)readObjectForKey:(NSString *)key;
+ (void)deleteObjectForKey:(NSString *)key;
+ (void)deleteAllObject;
@end
@interface SaveKeyChain : NSObject
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service;
+ (void)save:(NSString *)service data:(id)data;
+ (id)load:(NSString *)service;
+ (void)delete:(NSString *)service;
@end
Copy the code

obf_KeyChain_fuc.m

#import "obf_KeyChain_fuc.h" #define BUNDLE_ID [[NSBundle mainBundle] bundleIdentifier] @implementation obf_KeyChain_fuc  + (void)saveObject:(id)object forKey:(NSString *)key { NSMutableDictionary *mDic = [NSMutableDictionary dictionary]; [mDic setValuesForKeysWithDictionary:(NSMutableDictionary *)[SaveKeyChain load:BUNDLE_ID]]; [mDic setObject:object forKey:key]; [SaveKeyChain save:BUNDLE_ID data:mDic]; } + (id)readObjectForKey:(NSString *)key { NSMutableDictionary *mDic = (NSMutableDictionary *)[SaveKeyChain load:BUNDLE_ID]; return [mDic objectForKey:key]; } + (void)deleteObjectForKey:(NSString *)key { NSMutableDictionary *mDic = (NSMutableDictionary *)[SaveKeyChain load:BUNDLE_ID]; [mDic removeObjectForKey:key]; [SaveKeyChain save:BUNDLE_ID data:mDic]; } + (void)deleteAllObject { [SaveKeyChain delete:BUNDLE_ID]; } @end @implementation SaveKeyChain + (NSMutableDictionary *)getKeychainQuery:(NSString *)service { return [NSMutableDictionary dictionaryWithObjectsAndKeys: (__bridge_transfer id)kSecClassGenericPassword,(__bridge_transfer id)kSecClass, service, (__bridge_transfer id)kSecAttrService, service, (__bridge_transfer id)kSecAttrAccount, (__bridge_transfer id)kSecAttrAccessibleAfterFirstUnlock,(__bridge_transfer id)kSecAttrAccessible, nil]; } + (void)save:(NSString *)service data:(id)data { //Get search dictionary NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; //Delete old item before add new item SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery); //Add new object to search dictionary(Attention:the data format) [keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge_transfer id)kSecValueData]; //Add item to keychain with the search dictionary SecItemAdd((__bridge_retained CFDictionaryRef)keychainQuery, NULL); } + (id)load:(NSString *)service { id ret = nil; NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; //Configure the search setting [keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge_transfer id)kSecReturnData];  [keychainQuery setObject:(__bridge_transfer id)kSecMatchLimitOne forKey:(__bridge_transfer id)kSecMatchLimit]; CFDataRef keyData = NULL; if (SecItemCopyMatching((__bridge_retained CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) { @try { ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge_transfer NSData *)keyData]; } @catch (NSException *e) { NSLog(@"Unarchive of %@ failed: %@", service, e); } @finally { } } return ret; } + (void)delete:(NSString *)service { NSMutableDictionary *keychainQuery = [self getKeychainQuery:service]; SecItemDelete((__bridge_retained CFDictionaryRef)keychainQuery); } @endCopy the code

Ios14 needs to add add KeychainSharing

Click +Capability on the Signing & Capabilities screen and double-click the key icon

Click the + sign below the Keychain Groups box to automatically fill in the package name.