获取UUID两种方法,并让UUID唯一的方法

时间:2021-07-18 19:28:16

方法一、

CFUUIDRefpuuid = CFUUIDCreate(nil);

    CFStringRefuuidString = CFUUIDCreateString(nil, puuid);

    NSString*result = (NSString*)CFBridgingRelease(CFStringCreateCopy(NULL, uuidString));

    CFRelease(puuid);

    CFRelease(uuidString);

return  result;

方法二、

NSString*myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; 



UUID 重装后改变;


为了确保UUID唯一, 需要使用更为安全的keychain

下载SSKeychain


#import "SSKeychain.h"

#pragma mark- 获取UUID

- (NSString *)getIOSUUID

{

    NSString *retrieveuuid = [SSKeychainpasswordForService:@"xxxxx"account:@"uuid"];

    

   if ( retrieveuuid == nil || [retrieveuuid isEqualToString:@""]){

       CFUUIDRef uuid = CFUUIDCreate(NULL);

       assert(uuid != NULL);

       CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);

        

        retrieveuuid = [NSStringstringWithFormat:@"%@", uuidStr];

        

        [SSKeychainsetPassword:retrieveuuid forService:@"xxxxx" account:@"uuid"];

    }

   NSLog(@"UUID: %@", retrieveuuid);

   return retrieveuuid;

}