供应商标识符在iOS 5.0版中不起作用

NIS*_* gp 2 uniqueidentifier ios ios5 ios6

我正在使用供应商标识符,因为不推荐使用UDID,但使用此应用程序后,在iOS版本低于6.0的设备中无效.谁能告诉我需要做什么?该应用程序是否仅在iOS 6.0及更高版本中受支持?如何在iOS版本低于6.0的设备中使用我的应用程序功能?以下是我正在使用的代码:

static NSString *uniqueIdentifier = nil;
id vendorIdObject = [[UIDevice currentDevice] identifierForVendor];
uniqueIdentifier = [vendorIdObject UUIDString];
Run Code Online (Sandbox Code Playgroud)

Jas*_*oco 6

您可以执行以下操作(请注意,此返回的标识符未绑定到设备,如果用户删除并重新安装应用程序,则会更改):

NSString* uniqueIdentifier = nil;
if( [UIDevice instancesRespondToSelector:@selector(identifierForVendor)] ) {
  // iOS 6+
  uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
} else {
  // before iOS 6, so just generate an identifier and store it
  uniqueIdentifier = [[NSUserDefaults standardUserDefaults] objectForKey:@"identiferForVendor"];
  if( !uniqueIdentifier ) {
    CFUUIDRef uuid = CFUUIDCreate(NULL);
    uniqueIdentifier = (__bridge_transfer NSString*)CFUUIDCreateString(NULL, uuid);
    CFRelease(uuid);
    [[NSUserDefaults standardUserDefaults] setObject:uniqueIdentifier forKey:@"identifierForVendor"];
  }
}
Run Code Online (Sandbox Code Playgroud)

这将生成iOS-6之前的标识符并将其存储为默认值,以便它通常是相同的标识符.如果您有一个钥匙串组和一套需要以类似方式使用标识符的应用程序identifierForVendor,您可以将其存放在钥匙串中而不是用户默认值中.