刚刚发现,UIDevice uniqueIdentifier属性在iOS 5中已弃用,在iOS 7及以上版本中不可用。似乎没有可供选择的方法或属性。

我们现有的许多应用程序都紧密依赖于这个属性来唯一地识别特定的设备。今后我们该如何处理这个问题?

2011-2012年的文件建议:

特殊注意事项 不要使用uniqueIdentifier属性。创建特定的唯一标识符 你可以调用CFUUIDCreate函数来创建一个UUID,然后写入 使用NSUserDefaults类将它转换到默认数据库。

但是,如果用户卸载和重新安装应用程序,这个值就不一样了。


当前回答

我也遇到了一些问题,解决方法很简单:

    // Get Bundle Info for Remote Registration (handy if you have more than one app)
    NSString *appName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];


    // Get the users Device Model, Display Name, Unique ID, Token & Version Number
    UIDevice *dev = [UIDevice currentDevice];
    NSString *deviceUuid=[dev.identifierForVendor  UUIDString];

    NSString *deviceName = dev.name;

其他回答

可以帮助: 使用下面的代码,它将永远是唯一的,除非你擦除(格式)你的设备。

objective - c:

选项1:这将在每次安装时更改

UIDevice *uuid = [NSUUID UUID].UUIDString;

选项2:这将是唯一的每个供应商/开发者苹果帐户

UIDevice *myDevice = [UIDevice currentDevice];
NSString *uuid = [[myDevice identifierForVendor] UUIDString];

斯威夫特5。X:

选项1:这将在每次安装时更改

let uuid = UUID().uuidString

选项2:这将是唯一的每个供应商/开发者苹果帐户

let myDevice = UIDevice.current
let uuid = myDevice.identifierForVendor?.uuidString

不要使用这些库——libOmnitureAppMeasurement,它使用了苹果不再支持的uniqueIdentifier

苹果在iOS 11中添加了一个名为DeviceCheck的新框架,它将帮助你非常容易地获得唯一标识符。 阅读此表格了解更多信息。 https://medium.com/@santoshbotre01/unique-identifier-for-the-ios-devices-590bb778290d

创建自己的UUID,然后将其存储在Keychain中。因此,即使你的应用程序被卸载,它仍然存在。在许多情况下,即使用户在设备之间迁移(例如完全备份并恢复到另一个设备),它也会持续存在。

实际上,就您所关心的而言,它成为唯一的用户标识符。(甚至比设备标识符更好)。

例子:

我正在定义一个自定义方法来创建一个UUID:

- (NSString *)createNewUUID 
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

然后,你可以在应用的第一次启动时将它存储在KEYCHAIN中。因此,在第一次启动后,我们可以简单地从KEYCHAIN中使用它,不需要重新生成它。使用Keychain存储的主要原因是:当你将UUID设置为Keychain时,即使用户完全卸载了应用程序,然后重新安装它,它也会持续存在。这是存储它的永久方式,这意味着键会一直是唯一的。

     #import "SSKeychain.h"
     #import <Security/Security.h>

在应用程序启动包括以下代码:

 // getting the unique key (if present ) from keychain , assuming "your app identifier" as a key
       NSString *retrieveuuid = [SSKeychain passwordForService:@"your app identifier" account:@"user"];
      if (retrieveuuid == nil) { // if this is the first time app lunching , create key for device
        NSString *uuid  = [self createNewUUID];
// save newly created key to Keychain
        [SSKeychain setPassword:uuid forService:@"your app identifier" account:@"user"];
// this is the one time process
}

下载SSKeychain。m和.h文件从sskeychain和拖动sskeychain。m和.h文件到你的项目,并添加“Security.framework”到你的项目。 要使用UUID,只需使用:

NSString *retrieveuuid = [SSKeychain passwordForService:@"your app identifier" account:@"user"];

这是我用来获取iOS 5和iOS 6,7的ID的代码:

- (NSString *) advertisingIdentifier
{
    if (!NSClassFromString(@"ASIdentifierManager")) {
        SEL selector = NSSelectorFromString(@"uniqueIdentifier");
        if ([[UIDevice currentDevice] respondsToSelector:selector]) {
            return [[UIDevice currentDevice] performSelector:selector];
        }
    }
    return [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
}