How to get the Device ID from an iOS app?
How can I get the unique ID o开发者_JAVA技巧f the user's device in an iOS app?
use this
UIDevice *device = [UIDevice currentDevice];
NSString *uniqueIdentifier = [device uniqueIdentifier];
Update
Apple has the deprecated unique identifier, so now the following code (from Melvin Sovereign's comment) is appropriate:
NSString *uniqueIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
Interestingly, Apple has since deprecated the uniqueIdentifier in iOS 5. Here's the relevant TechCrunch article: http://techcrunch.com/2011/08/19/apple-ios-5-phasing-out-udid/
Apple suggests that you no longer uniquely identify the device but instead identify the user. In most cases, this is excellent advice though there are some situations which still require a globally unique device ID. These scenarios are quite common in advertising. Hence, I wrote an extremely simple drop-in library which replicates the existing behavior exactly.
In a shameless plug of self promotion, I'll link it here in the hope that someone finds it useful. Also, I welcome all and any feedback/criticism: http://www.binpress.com/app/myid/591
I think this code may help you ;)
NSString * id = [UIDevice currentDevice].uniqueIdentifier;
You can also have a look to http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html
You can use NSUUID *identifierForVendor
[[UIDevice currentDevice] identifierForVendor]
In Swift
var uniqueId=UIDevice.currentDevice().identifierForVendor.UUIDString as String
println("Your device identifires =>\(uniqueId)")
I know, that this is a pretty old question, but this is how i solve the problem. It might fail, if the thread creating the "unique device token" never stops, but it worked for me.
-(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;
}
精彩评论