ios 如何使用 Objective-c 为 iPhone/iPad 生成设备的唯一 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41016762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate Unique ID of device for iPhone/iPad using Objective-c
提问by Poonam K.
I wanted to know that How to generate Unique ID of device for iPhone/iPad using Objective-c
我想知道如何使用 Objective-c 为 iPhone/iPad 生成设备的唯一 ID
so that once application is installed on device , so we should track that deviceID
这样一旦应用程序安装在设备上,我们就应该跟踪该设备 ID
I have searched to retrieve IMEI of iPhone/iPad , but it is not allowed in objective-c.
Then I searched to generate UDID of iPhone/iPad but it is generating for different ID each time I launched it on simulator .
我已经搜索以检索 iPhone/iPad 的 IMEI,但在 Objective-c 中是不允许的。
然后我搜索生成 iPhone/iPad 的 UDID,但每次我在模拟器上启动它时它都会生成不同的 ID。
回答by badhanganesh
Yes, UDIDis deprecated; we are not allowed to get UDID due to user privacy purposes. Apple does not allow to get any identifiers that uniquely identifies a device, such as IMEI, MAC address, UDIDetc.
是的,UDID已被弃用;出于用户隐私目的,我们不允许获取 UDID。Apple 不允许获取任何唯一标识设备的标识符,例如IMEI、MAC 地址、UDID等。
UUIDis the best way to go as of now. But that would be unique for each vendor. You are not assured that it will be unique each time you get the UUIDstring. Best bet is to store the UUIDstring to phone's Keychainand retrieve it when needed, with a catch. When you factory-reset your phone, the keychain items would be erased. This limitation should be kept in mind.
UUID是目前最好的方法。但这对于每个供应商来说都是独一无二的。您不能保证每次获得UUID字符串时它都是唯一的。最好的办法是将UUID字符串存储到手机的钥匙串中,并在需要时检索它,并抓住。当您将手机恢复出厂设置时,钥匙串项目将被删除。应牢记此限制。
UPDATE - IN IOS 10.3 BETA'S:
更新 - 在 IOS 10.3 测试版中:
It seems that Apple has made some changes to how Keychain works in iOS 10.3+. Keychain items stored in the Keychain will be deletedwhen the all the apps from the specific vendor are uninstalled. According to Apple, the residence of sensitive information of an app even after the app is gone from the device may lead to security risks, so they decided to forbid this kind of behavior.
Apple 似乎对 Keychain 在iOS 10.3+ 中的工作方式进行了一些更改。当卸载特定供应商的所有应用程序时,存储在钥匙串中的钥匙串项目将被删除。苹果公司表示,即使应用程序从设备上消失,应用程序的敏感信息仍可能存在安全隐患,因此他们决定禁止这种行为。
Developers relying on Keychain storage even after an uninstall for their apps can make use of this WORKAROUNDto continue with the intended functionality. According to this workaround, any app can access the information stored in that specific Keychain Access Group, so it is recommended that adding an extra layer of encryption to your data will protect it with even more security, although keychain encrypts items by default.
即使在卸载应用程序后依赖钥匙串存储的开发人员也可以利用此变通办法继续使用预期功能。根据此变通方法,任何应用程序都可以访问存储在该特定钥匙串访问组中的信息,因此建议为您的数据添加额外的加密层将保护它的安全性更高,尽管钥匙串默认情况下会加密项目。
UPDATE - IOS 10.3.3 (STABLE):It seems that the keychain items deletion was a BUG in early betas of iOS 10.3.3 and was fixed later in the stable release. This might have been caused during betas since strange things can happen during that phase. It should be no problem to use Keychain hereafter.
更新 - IOS 10.3.3(稳定版):在 iOS 10.3.3 的早期测试版中,钥匙串项目的删除似乎是一个错误,并在稳定版本的后期修复。这可能是在测试版期间引起的,因为在那个阶段可能会发生奇怪的事情。以后用Keychain应该没问题了。
回答by Asaduzzaman Shuvro
You can use UUID (Universal User Identification). Following link contains apple documentation
您可以使用 UUID(通用用户标识)。以下链接包含苹果文档
https://developer.apple.com/reference/uikit/uidevice/1620059-identifierforvendor
https://developer.apple.com/reference/uikit/uidevice/1620059-identifierforvendor
you can use this code for UUID:
您可以将此代码用于 UUID:
//Objective-C
//目标-C
NSString * string = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
//Swift
//迅速
let deviceID = UIDevice.currentDevice().identifierForVendor?.UUIDString
//Swift 3
//斯威夫特3
let deviceID = UIDevice.current.identifierForVendor?.uuidString
回答by SM18
Use the below code to get UDID for iOS device Use KeychainItemWrapper Class download from URL KeychainItemWrap
使用以下代码获取 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)];
}
回答by nelsonenzo
Anyone coming here post 2017, Apple has implemented DeviceCheck for this purposes.
任何人在 2017 年之后来到这里,Apple 已经为此实施了 DeviceCheck。
https://developer.apple.com/documentation/devicecheck#overview
https://developer.apple.com/documentation/devicecheck#overview
You can use DeviceCheck to see if this particular device has installed or used your particular app. It doesn't give you a unique id to the phone, but it does allow you to see if a user has burned through a promotion or not.
您可以使用 DeviceCheck 查看此特定设备是否已安装或使用您的特定应用程序。它不会为您提供电话的唯一 ID,但它确实允许您查看用户是否已通过促销活动。
回答by Sagar Snehi
You cannot take IMEI and phone number of user mobile, Apple is restricted to get these uniqueID's.
您不能获取用户手机的 IMEI 和电话号码,Apple 只能获取这些唯一 ID。
You have to store UDID in keychain. for this you have to download keychainwrapper class and store the UDID generated by above code:
您必须将 UDID 存储在钥匙串中。为此,您必须下载 keychainwrapper 类并存储由上述代码生成的 UDID:
UIDevice *device = [[UIDevice alloc]init];
NSString *idForVend = [NSString stringWithFormat:@"%@", [device identifierForVendor]];
NSLog(@"Identifier For Vendor : %@",idForVend);
follow this link it will solve your problem for sure: https://stackoverflow.com/questions/16459879/how-to-store-a-string-in-keychain-ios, iOS? If you use this If you delete app And install again it will remain same DeviceID.
按照此链接,它肯定会解决您的问题:https://stackoverflow.com/questions/16459879/how-to-store-a-string-in-keychain-ios,iOS?如果您使用它 如果您删除应用程序并再次安装,它将保持相同的 DeviceID。
回答by Ofir Malachi
#import <AdSupport/AdSupport.h>
NSString* sClientID = [[[ASIdentifierManager sharedManager]advertisingIdentifier] UUIDString];
The Best UUIDbecause:
在最佳UUID是因为:
- it will never change (*even if the app will be deleted)
- Apple approve it.
- 它永远不会改变(*即使应用程序将被删除)
- 苹果批准它。
回答by sohil
Solution is :
解决方案是:
You can do it with DeviceToken
. DeviceToken are uniq for all mobile.
你可以用DeviceToken
. DeviceToken 是适用于所有移动设备的 uniq。
Code is here :
代码在这里:
- (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);
}
Device token are uniq for all device.
设备令牌对于所有设备都是唯一的。