找回密码
 立即注册
查看: 314|回复: 0

unity iOS获取UUID

[复制链接]
发表于 2022-6-8 08:09 | 显示全部楼层 |阅读模式
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 <Foundation/Foundation.h> @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 <UIKit/UIKit.h> 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//获取UUIDconst 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开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-24 20:45 , Processed in 0.089499 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表