我怎样才能在iOS应用程序中获取用户设备的唯一标识符?
使用这个
UIDevice *device = [UIDevice currentDevice];
NSString *uniqueIdentifier = [device uniqueIdentifier];
更新
由于苹果公司已经废弃了唯一标识符,所以现在以下代码(来自Melvin Sovereign的评论)是合适的:
NSString *uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
我认为这段代码可能对你有帮助;)
NSString * id = [UIDevice currentDevice].uniqueIdentifier;
var uniqueId=UIDevice.currentDevice().identifierForVendor.UUIDString as String
println("Your device identifires =>\(uniqueId)")
NSUUID *identifierForVendor
。[[UIDevice currentDevice] identifierForVendor]
我知道这是一个相当陈旧的问题,但这是我解决问题的方式。如果创建“唯一设备令牌”的线程永远不停止,则可能会失败,但对我而言它有效。
-(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;
}
[[[UIDevice currentDevice] identifierForVendor] UUIDString]
- Melvin SovereignUUIDString
应该被缓存吗?还是获取它的成本很低廉? - 最白目