213

I want to be able to create a GUID/UUID on the iPhone and iPad.

The intention is to be able to create keys for distributed data that are all unique. Is there a way to do this with the iOS SDK?

Cœur
  • 37,241
  • 25
  • 195
  • 267
rustyshelf
  • 44,963
  • 37
  • 98
  • 104
  • 3
    There's an article about how to ensure you never lose that generated UUID, even for iOS 5 and later using the KeyChain. https://blog.onliquid.com/persistent-device-unique-identifier-ios-keychain/ – jasoares May 28 '15 at 16:44

8 Answers8

325
[[UIDevice currentDevice] uniqueIdentifier]

Returns the Unique ID of your iPhone.

EDIT: -[UIDevice uniqueIdentifier] is now deprecated and apps are being rejected from the App Store for using it. The method below is now the preferred approach.

If you need to create several UUID, just use this method (with ARC):

+ (NSString *)GetUUID
{
  CFUUIDRef theUUID = CFUUIDCreate(NULL);
  CFStringRef string = CFUUIDCreateString(NULL, theUUID);
  CFRelease(theUUID);
  return (__bridge NSString *)string;
}

EDIT: Jan, 29 2014: If you're targeting iOS 6 or later, you can now use the much simpler method:

NSString *UUID = [[NSUUID UUID] UUIDString];
Olie
  • 24,597
  • 18
  • 99
  • 131
Stephan Burlot
  • 5,077
  • 3
  • 25
  • 26
  • 21
    [[UIDevice currentDevice] uniqueIdentifier] has been deprecated as of iOS 5, and Apple is now rejecting apps that use it (as of March 2012). The rest of the response is still accurate. – Gorm Mar 28 '12 at 20:15
  • 13
    Having been around this question for a while, I'm pretty sure the original question wasn't about the device's unique identifier, but about just getting a useful general unique identifier for something. – Ryan McCuaig Apr 12 '12 at 03:43
  • 12
    To make this ARC compliant you must cast the string object and remove the autorelease call. In other words add the cast: NSString* string = (__bridge_transfer NSString*)CFUUIDCreateString(NULL, theUUID); – Paul de Lange May 07 '12 at 08:05
  • 66
    As of iOS 6 you can use: NSString *UUID = [[NSUUID UUID] UUIDString]; – Symmetric Nov 19 '12 at 18:36
  • Is the auto-release needed? Xcode complains about it when I run "Analyze", that's all. It says "Object with a +0 retain count returned to caller where a +1 (owning) retain count is expected"| – Marc Feb 08 '13 at 11:15
  • The correct answer for iOS5 and 6 with XCode 4 or later is nanshi's getUUID implementation. – GaryO Jun 09 '13 at 20:25
  • 1
    If i use [[NSUUID UUID] UUIDString]; in iOS7 and above, won't my app rejected by Apple? – Whoami Sep 09 '14 at 07:25
  • Any idea how to use this in swift? – jcpennypincher Sep 04 '15 at 22:38
  • iOS9: NSString *UUID = [[NSUUID UUID] UUIDString] UUID always results in 'nil' why is this? – Quadrivium Oct 01 '15 at 19:41
  • @Quadrivium I just used it on an iOS 8 + 9 app and it works just fine. I even re-tested it for you a minute ago :) – marsbear Apr 30 '16 at 18:08
  • @Whoami No Apple won't reject your app because of this. That code just generates a unique id and nothing more (and one unique id every time you call it). It is not the device's unique id. – marsbear Apr 30 '16 at 18:09
  • Xcode reports potential leak with the first example, and suggests using `CFBridgingRelease`: `CFUUIDRef UUID = CFUUIDCreate(NULL); CFStringRef stringUUID = CFUUIDCreateString(NULL, UUID); CFRelease(UUID); return (NSString *)CFBridgingRelease(stringUUID);` – Vlad Serhiienko Aug 02 '19 at 09:30
101

Here is the simple code I am using, compliant with ARC.

+(NSString *)getUUID
{
    CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
    NSString * uuidString = (__bridge_transfer NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
    CFRelease(newUniqueId);

    return uuidString;
}
trillions
  • 3,669
  • 10
  • 40
  • 59
  • 36
    Honestly this should be the accepted answer, the original question asked for a GUID or UUID generator, and everyone responded with how to obtain the phone's UDID. A Universally Unique Identifier (UUID) and Unique Device Identifier (UDID) are not the same thing. – Joe Mar 11 '13 at 01:51
  • 1
    here is a straight line one NSString *UUID = [[NSUUID UUID] UUIDString]; – Adeel Aug 31 '16 at 09:11
93

In iOS 6 you can easily use:

NSUUID  *UUID = [NSUUID UUID];
NSString* stringUUID = [UUID UUIDString];

More details in Apple's Documentations

Arian Sharifian
  • 1,295
  • 11
  • 14
46

Reviewing the Apple Developer documentation I found the CFUUID object is available on the iPhone OS 2.0 and later.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Henk
  • 1,704
  • 10
  • 12
24

In Swift:

var uuid: String = NSUUID().UUIDString
println("uuid: \(uuid)")
King-Wizard
  • 15,628
  • 6
  • 82
  • 76
22

The simplest technique is to use NSString *uuid = [[NSProcessInfo processInfo] globallyUniqueString]. See the NSProcessInfo class reference.

Ryan McCuaig
  • 2,229
  • 15
  • 21
  • 8
    That returns a unique identifier for the process. It has no resemblance to a GUID. From the docs: "The ID includes the host name, process ID, and a time stamp, which ensures that the ID is unique for the network" – Matt Spradley May 01 '11 at 02:21
  • You could be right. It's a pragmatic one-liner, and probably isn't as bulletproof as CFUUID. Anyone who needs the greater rigour ought to drop down to CoreFoundation. – Ryan McCuaig May 06 '11 at 18:51
20

In Swift 3.0

var uuid = UUID().uuidString
Radu Diță
  • 13,476
  • 2
  • 30
  • 34
7

I've uploaded my simple but fast implementation of a Guid class for ObjC here: obj-c GUID

Guid* guid = [Guid randomGuid];
NSLog("%@", guid.description);

It can parse to and from various string formats as well.

NANNAV
  • 4,875
  • 4
  • 32
  • 50
tumtumtum
  • 1,052
  • 9
  • 11