【发布时间】:2011-03-29 06:17:12
【问题描述】:
如何在 iOS 应用中获取用户设备的唯一 ID?
【问题讨论】:
标签: ios objective-c iphone
如何在 iOS 应用中获取用户设备的唯一 ID?
【问题讨论】:
标签: ios objective-c iphone
使用这个
UIDevice *device = [UIDevice currentDevice];
NSString *uniqueIdentifier = [device uniqueIdentifier];
更新
Apple 具有已弃用的唯一标识符,因此现在以下代码(来自 Melvin Sovereign 的评论)是合适的:
NSString *uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
【讨论】:
[[[UIDevice currentDevice] identifierForVendor] UUIDString]
UUIDString 还是获取成本低廉?
有趣的是,Apple 已经在 iOS 5 中弃用了 uniqueIdentifier。下面是相关的 TechCrunch 文章: http://techcrunch.com/2011/08/19/apple-ios-5-phasing-out-udid/
Apple 建议您不再唯一标识设备,而是标识用户。在大多数情况下,这是一个很好的建议,尽管在某些情况下仍需要全局唯一的设备 ID。这些场景在广告中很常见。因此,我编写了一个非常简单的插件库,它准确地复制了现有的行为。
在一个无耻的自我推销中,我将它链接在这里,希望有人觉得它有用。此外,我欢迎所有和任何反馈/批评: http://www.binpress.com/app/myid/591
【讨论】:
我认为这段代码可能会对你有所帮助;)
NSString * id = [UIDevice currentDevice].uniqueIdentifier;
【讨论】:
您可以使用NSUUID *identifierForVendor
[[UIDevice currentDevice] identifierForVendor]
【讨论】:
在斯威夫特中
var uniqueId=UIDevice.currentDevice().identifierForVendor.UUIDString as String
println("Your device identifires =>\(uniqueId)")
【讨论】:
我知道,这是一个很老的问题,但这就是我解决问题的方法。如果创建“唯一设备令牌”的线程永远不会停止,它可能会失败,但它对我有用。
-(NSString*)getUniqueDeviceToken
{
__block NSString* UDTToReturn = @"UDTCouldNotBeCreatedSuccessfully,PlatformNotSupported,Simulator,iOSVer<11";
dispatch_semaphore_t semaphoretowaitforudtcreation = dispatch_semaphore_create(0);
if ([DCDevice.currentDevice isSupported])
{
[DCDevice.currentDevice generateTokenWithCompletionHandler:^(NSData * _Nullable token, NSError * _Nullable error)
{
if (error)
{
UDTToReturn = error.description;
dispatch_semaphore_signal(semaphoretowaitforudtcreation);
}
else
{
UDTToReturn = [token base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
dispatch_semaphore_signal(semaphoretowaitforudtcreation);
}
}];
}
dispatch_semaphore_wait(semaphoretowaitforudtcreation, DISPATCH_TIME_FOREVER);
return UDTToReturn;
}
【讨论】: