iOS 获取设备唯一标示符的方法详解

2020-01-21 00:36:50王冬梅


//保存一个UUID字符串到钥匙串:
CFUUIDRef uuid = CFUUIDCreate(NULL);
assert(uuid != NULL);
CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
 [SAMKeychain setPassword: [NSString stringWithFormat:@"%@", uuidStr]
 forService:@"com.yourapp.yourcompany"account:@"user"];

//从钥匙串读取UUID:
NSString *retrieveuuid = [SAMKeychain passwordForService:@"com.yourapp.yourcompany"account:@"user"];

**注意: setPassword和passwordForSevice方法中的**services 和 accounts 参数应该是一致的。

更多详细用法说明可以看SAMKeyChains Documentation

基本的实现思路便是这样,下面是具体的一种具体实现代码,仅供参考。


+ (NSString *)getDeviceId
{
  NSString * currentDeviceUUIDStr = [SAMKeychain passwordForService:@" "account:@"uuid"];
  if (currentDeviceUUIDStr == nil || [currentDeviceUUIDStr isEqualToString:@""])
  {
    NSUUID * currentDeviceUUID = [UIDevice currentDevice].identifierForVendor;
    currentDeviceUUIDStr = currentDeviceUUID.UUIDString;
    currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
    currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString];
    [SAMKeychain setPassword: currentDeviceUUIDStr forService:@" "account:@"uuid"];
  }
  return currentDeviceUUIDStr;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。、


注:相关教程知识阅读请移步到IOS开发频道。