unity iOS获取UUID

1.unity 新建UUIDToolExtern.cs

[DllImport("__Internal")]
    private static extern string _getUUIDInKeychain();
    public static string getUUIDInKeychain()
    {
       return _getUUIDInKeychain();
    }

2.ios 新建 UUIDTool.h

//
//  UUIDTool.h
//  Unity-iPhone
//
//  Created by zhangyu on 2022/6/7.
//

#import 
 
@interface UUIDTool : NSObject
 
/**
 * 普通的获取UUID的方法
 */
+ (NSString *)getUUID;
 
/**
 * 获取到UUID后存入系统中的keychain中,保证以后每次可以得到相同的唯一标志
 * 不用添加plist文件,当程序删除后重装,仍可以得到相同的唯一标示
 * 但是当系统升级或者刷机后,系统中的钥匙串会被清空,再次获取的UUID会与之前的不同
 * @return keychain中存储的UUID
 */
+ (NSString *)getUUIDInKeychain;
 
/**
 * 删除存储在keychain中的UUID
 * 如果删除后,重新获取用户的UUID会与之前的UUID不同
 */
+ (void)deleteKeyChain;
 
 
@end


3.ios 新建UUIDTool.m

//
//  UUIDTool.m
//  Unity-iPhone
//
//  Created by zhangyu on 2022/6/7.
//

#import "UUIDTool.h"
#import 
 
NSString * const kUUIDKey = @"com.QuercusRobur.followers";
 
@implementation UUIDTool
 
// 普通的获取UUID的方法
+ (NSString *)getUUID {
    CFUUIDRef puuid = CFUUIDCreate( nil );
    CFStringRef uuidString = CFUUIDCreateString(nil, puuid);
    NSString *result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
    return result;
}
 
 
#pragma mark - 获取到UUID后存入系统中的keychain中
 
+ (NSString *)getUUIDInKeychain {
    // 1.直接从keychain中获取UUID
    NSString *getUDIDInKeychain = (NSString *)[UUIDTool load:kUUIDKey];
    NSLog(@"从keychain中获取UUID%@", getUDIDInKeychain);
    
    // 2.如果获取不到,需要生成UUID并存入系统中的keychain
    if (!getUDIDInKeychain || [getUDIDInKeychain isEqualToString:@""] || [getUDIDInKeychain isKindOfClass:[NSNull class]]) {
        // 2.1 生成UUID
        CFUUIDRef puuid = CFUUIDCreate(nil);
        CFStringRef uuidString = CFUUIDCreateString(nil, puuid);
        NSString *result = (NSString *)CFBridgingRelease(CFStringCreateCopy(NULL, uuidString));
        CFRelease(puuid);
        CFRelease(uuidString);
        NSLog(@"生成UUID:%@",result);
        // 2.2 将生成的UUID保存到keychain中
        [UUIDTool save:kUUIDKey data:result];
        // 2.3 从keychain中获取UUID
        getUDIDInKeychain = (NSString *)[UUIDTool load:kUUIDKey];
    }
    
    return getUDIDInKeychain;
}
 
 
#pragma mark - 删除存储在keychain中的UUID
 
+ (void)deleteKeyChain {
    [self delete:kUUIDKey];
}
 
 
#pragma mark - 私有方法
 
+ (NSMutableDictionary *)getKeyChainQuery:(NSString *)service {
    return [NSMutableDictionary dictionaryWithObjectsAndKeys:(id)kSecClassGenericPassword,(id)kSecClass,service,(id)kSecAttrService,service,(id)kSecAttrAccount,(id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible, nil];
}
 
// 从keychain中获取UUID
+ (id)load:(NSString *)service {
    id ret = nil;
    NSMutableDictionary *keychainQuery = [self getKeyChainQuery:service];
    [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 *exception) {
            NSLog(@"Unarchive of %@ failed: %@", service, exception);
        }
        @finally {
            NSLog(@"finally");
        }
    }
    
    if (keyData) {
        CFRelease(keyData);
    }
    NSLog(@"ret = %@", ret);
    return ret;
}
 
+ (void)delete:(NSString *)service {
    NSMutableDictionary *keychainQuery = [self getKeyChainQuery:service];
    SecItemDelete((CFDictionaryRef)keychainQuery);
}
 
// 将生成的UUID保存到keychain中
+ (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 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);
}
 
 
@end
#if defined (__cplusplus)
extern "C" {
#endif
//获取UUID
const char * _getUUIDInKeychain()
{
    NSString *s = [UUIDTool getUUIDInKeychain];
    const char *cString = [s UTF8String];
    NSLog(@"我获得的UUID%@",s);
    return strdup(cString);
}

# if defined (__cplusplus)
}
#endif

4.需要特别设置iOS


image.png

你可能感兴趣的:(unity iOS获取UUID)