super1 发表于 2022-6-8 08:09

unity iOS获取UUID

1.unity 新建UUIDToolExtern.cs
    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 *);    NSLog(@"从keychain中获取UUID%@", getUDIDInKeychain);      // 2.如果获取不到,需要生成UUID并存入系统中的keychain    if (!getUDIDInKeychain || || ]) {      // 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中      ;      // 2.3 从keychain中获取UUID      getUDIDInKeychain = (NSString *);    }      return getUDIDInKeychain;}#pragma mark - 删除存储在keychain中的UUID + (void)deleteKeyChain {    ;}#pragma mark - 私有方法 + (NSMutableDictionary *)getKeyChainQuery:(NSString *)service {    return ;} // 从keychain中获取UUID+ (id)load:(NSString *)service {    id ret = nil;    NSMutableDictionary *keychainQuery = ;    ;    ;    CFDataRef keyData = NULL;    if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {      @try {            ret = ;      }      @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 = ;    SecItemDelete((CFDictionaryRef)keychainQuery);} // 将生成的UUID保存到keychain中+ (void)save:(NSString *)service data:(id)data {    // Get search dictionary    NSMutableDictionary *keychainQuery = ;    // Delete old item before add new item    SecItemDelete((CFDictionaryRef)keychainQuery);    // Add new object to search dictionary(Attention:the data format)    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 = ;    const char *cString = ;    NSLog(@"我获得的UUID%@",s);    return strdup(cString);}# if defined (__cplusplus)}#endif
4.需要特别设置iOS


image.png
页: [1]
查看完整版本: unity iOS获取UUID