如何使用Objective-c为iPhone / iPad生成设备的唯一ID

我想知道如何使用Objective-c为iPhone / iPad生成设备的唯一ID

因此,一旦应用程序安装在设备上,我们就应该跟踪该deviceID

  • 我已经搜索过检索iPhone / iPad的IMEI,但是在objective-c中不允许这样做。

  • 然后我搜索生成iPhone / iPad的UDID,但每次我在模拟器上启动它时都会生成不同的ID。

是的,不推荐使用UDID ; 由于用户隐私目的,我们不允许获取UDID。 Apple不允许获取唯一标识设备的标识符,例如IMEI,MAC地址,UDID等。

UUID是目前最好的方式。 但这对每个供应商来说都是独一无二的。 每次获得UUID字符串时,您都不确定它是唯一的。 最好的办法是将UUID字符串存储到手机的钥匙串中 ,并在需要时使用catch来检索它。 当您出厂重置手机时,钥匙串项目将被删除。 应该牢记这个限制。

更新 – 在IOS 10.3 BETA中:

似乎Apple对Keychain在iOS 10.3+中的工作方式做了一些改变。 当卸载特定供应商的所有应用程序时,将删除存储在钥匙串中的钥匙串项目。 根据Apple的说法,即使应用程序从设备中消失,应用程序的敏感信息的驻留可能会导致安全风险,因此他们决定禁止这种行为。

即使在卸载应用程序后依赖Keychain存储的开发人员也可以使用此替代方法继续执行预期的function。 根据此解决方法,任何应用程序都可以访问存储在该特定Keychain访问组中的信息,因此建议为数据添加额外的加密层将以更高的安全性保护它,尽管默认情况下keychain会加密项目。

更新 – IOS 10.3.3(稳定):似乎在iOS 10.3.3的早期测试版中删除了钥匙串项目的BUG,并在稳定版本的后期修复。 这可能是在测试期间引起的,因为在该阶段可能会发生奇怪的事情。 以后使用钥匙串应该没问题。

您可以使用UUID(通用用户标识)。 以下链接包含Apple文档

https://developer.apple.com/reference/uikit/uidevice/1620059-identifierforvendor

https://developer.apple.com/library/content/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html#//apple_ref/doc/uid/TP40013162-SW1

您可以将此代码用于UUID:

// Objective-C的

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

//迅速

 let deviceID = UIDevice.currentDevice().identifierForVendor?.UUIDString 

//斯威夫特3

 let deviceID = UIDevice.current.identifierForVendor?.uuidString 

使用以下代码获取iOS设备的UDID使用KeychainItemWrapper从URL KeychainItemWrap下载类

 NSString *uuid; KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"AC_APP_UUID" accessGroup:nil]; NSString *keychainUUID = [keychain objectForKey:(__bridge id)(kSecAttrAccount)]; NSString *appVersion = [NSString stringWithFormat:@"%@",@"1.0"]; [keychain setObject:appVersion forKey:(__bridge id)(kSecAttrDescription)]; if (keychainUUID==nil||[keychainUUID isKindOfClass:[NSNull class]]||keychainUUID.length==0) { uuid = [[NSUUID UUID] UUIDString]; [keychain setObject:uuid forKey:(__bridge id)(kSecAttrAccount)]; }else{ uuid = [keychain objectForKey:(__bridge id)(kSecAttrAccount)]; } 

您无法获取用户手机的IMEI和电话号码,Apple仅限于获取这些uniqueID。

您必须将UDID存储在钥匙串中。 为此你必须下载keychainwrapper类并存储上面代码生成的UDID:

  UIDevice *device = [[UIDevice alloc]init]; NSString *idForVend = [NSString stringWithFormat:@"%@", [device identifierForVendor]]; NSLog(@"Identifier For Vendor : %@",idForVend); 

按照此链接,它将解决您的问题: https ://stackoverflow.com/questions/16459879/how-to-store-a-string-in-keychain-ios,iOS? 如果您使用此如果您删除应用程序并再次安装它将保持相同的DeviceID。

 #import  NSString* sClientID = [[[ASIdentifierManager sharedManager]advertisingIdentifier] UUIDString]; 

最好的UUID因为:

  1. 它永远不会改变(*即使应用程序将被删除)
  2. Apple批准了它。

解决方案是:

您可以使用DeviceToken完成此DeviceToken 。 对于所有移动设备,DeviceToken都是uniq。

代码在这里:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; } return yes; } - (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { NSString *AppDeviceToken=[[NSString alloc] initWithFormat:@"%@",deviceToken]; //NSLog(@"My token is: %@", self.AppDeviceToken); AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@" " withString:@""]; AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@"<" withString:@""]; AppDeviceToken = [self.AppDeviceToken stringByReplacingOccurrencesOfString:@">" withString:@""]; NSLog(@"%@'s Device Token is : %@",[[UIDevice currentDevice] name],AppDeviceToken); } - (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { NSLog(@"Failed to get token, error: %@", error); } 

设备令牌对于所有设备都是uniq。