How to generate UUID in ios

How to get the UUID in object c, for example, in Java, the UUID is used to generate unique random numbers that represent a 128-bit value.

+60
ios objective-c
Jan 16 '13 at 6:19 06:19
source share
8 answers

Try:

CFUUIDRef udid = CFUUIDCreate(NULL); NSString *udidString = (NSString *) CFUUIDCreateString(NULL, udid); 

UPDATE:

Starting with iOS 6, there is an easier way to generate UUIDs . And, as usual, there are several ways to do this:

Create a UUID string:

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

Create UUID:

 [[NSUUID UUID]; // which is same as.. [[NSUUID] alloc] init]; 

Creates an object of type NSConcreteUUID and can be easily added to NSString and looks like this: BE5BA3D0-971C-4418-9ECF-E2D1ABCB66BE

NOTE from the documentation:

Note. The NSUUID class is not a free bridge with CoreFoundations CFUUIDRef. Use UUID strings to convert between CFUUID and NSUUID, if necessary. Two NSUUIDs cannot be matched by pointer value (like CFUUIDRef); use isEqual: to compare two instances of NSUUID.

+133
Jan 16 '13 at 6:21
source share
 + (NSString *)uniqueFileName { CFUUIDRef theUniqueString = CFUUIDCreate(NULL); CFStringRef string = CFUUIDCreateString(NULL, theUniqueString); CFRelease(theUniqueString); return [(NSString *)string autorelease]; } 
+6
Jan 16 '13 at 6:21
source share

Quick version of Raptor answer :

 let uuid = NSUUID().UUIDString 
+4
Oct 08 '15 at 23:29
source share
 -(NSString*) myUUID() { CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault); CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID); NSString *guid = (__bridge NSString *)newUniqueIDString; CFRelease(newUniqueIDString); CFRelease(newUniqueID); return([guid lowercaseString]); } 
+3
Jan 16 '13 at 6:30
source share

you can use CFUUID for iOS 5 or lower and NSUUID for iOS 6 and 7. for greater security, you can store your UUID in the keychain

+1
Aug 6 '13 at 19:19
source share

I suggest you check out this library: https://github.com/fabiocaccamo/FCUUID

It provides a very simple API for obtaining universally unique identifiers with different levels of strength.

It is compatible with: iOS5, iOS6, iOS7, iOS8

+1
Oct. 06 '14 at 12:59 on
source share
 - (NSString*)generateGUID{ CFUUIDRef theUUID = CFUUIDCreate(NULL); CFStringRef string = CFUUIDCreateString(NULL, theUUID); CFRelease(theUUID); return [NSString stringWithFormat:@"%@", string]; } 
0
Jan 30 '17 at 13:56 on
source share
 #import <AdSupport/AdSupport.h> NSString* sClientID = [[[ASIdentifierManager sharedManager]advertisingIdentifier] UUIDString]; 

The best UUID because:

  • it will never change (* even if the application is uninstalled)
  • Apple approves of this.
-one
Aug 08 '17 at 7:32 on
source share



All Articles