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后存入系統(tǒng)中的keychain中撑瞧,保證以后每次可以得到相同的唯一標(biāo)志
* 不用添加plist文件评疗,當(dāng)程序刪除后重裝沾鳄,仍可以得到相同的唯一標(biāo)示
* 但是當(dāng)系統(tǒng)升級(jí)或者刷機(jī)后勋篓,系統(tǒng)中的鑰匙串會(huì)被清空,再次獲取的UUID會(huì)與之前的不同
* @return keychain中存儲(chǔ)的UUID
*/
+ (NSString *)getUUIDInKeychain;
/**
* 刪除存儲(chǔ)在keychain中的UUID
* 如果刪除后,重新獲取用戶的UUID會(huì)與之前的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后存入系統(tǒng)中的keychain中
+ (NSString *)getUUIDInKeychain {
// 1.直接從keychain中獲取UUID
NSString *getUDIDInKeychain = (NSString *)[UUIDTool load:kUUIDKey];
NSLog(@"從keychain中獲取UUID%@", getUDIDInKeychain);
// 2.如果獲取不到,需要生成UUID并存入系統(tǒng)中的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 - 刪除存儲(chǔ)在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.需要特別設(shè)置iOS