How to generate UUID in ios

IosObjective C

Ios Problem Overview


How to get a UUID in objective c, like in Java UUID is used to generate unique random numbers which represents 128 bit value.

Ios Solutions


Solution 1 - Ios

Try:

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

UPDATE:

As of iOS 6, there is an easier way to generate UUID. And as usual, there are multiple ways to do it:

Create a UUID string:

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

Create a UUID:

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

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

NOTE from the Documentation:

> Note: The NSUUID class is not toll-free bridged with CoreFoundation’s CFUUIDRef. Use UUID strings to convert between CFUUID and NSUUID, if needed. Two NSUUID objects are not guaranteed to be comparable by pointer value (as CFUUIDRef is); use isEqual: to compare two NSUUID instances.

Solution 2 - Ios

Swift version of Raptor's answer:

let uuid = UUID().uuidString

Solution 3 - Ios

+ (NSString *)uniqueFileName
{
    CFUUIDRef theUniqueString = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUniqueString);
    CFRelease(theUniqueString);
    return [(NSString *)string autorelease];
}

Solution 4 - Ios

-(NSString*) myUUID()
{
    CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
    CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID);
    NSString *guid = (__bridge NSString *)newUniqueIDString;
    CFRelease(newUniqueIDString);
    CFRelease(newUniqueID);
    return([guid lowercaseString]);
}

Solution 5 - Ios

you can use CFUUID for iOS 5 or lower version and NSUUID for iOS 6 and 7. for making it more secure you can store your UUID in keychain

Solution 6 - Ios

- (NSString*)generateGUID{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [NSString stringWithFormat:@"%@", string];
}

Solution 7 - Ios

For Swift 5.0, Use this,

    let uuidRef = CFUUIDCreate(nil)
    let uuidStringRef = CFUUIDCreateString(nil, uuidRef)
    let uuid = uuidStringRef as String? ?? ""
   

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaurabh JadhavView Question on Stackoverflow
Solution 1 - IosRaptorView Answer on Stackoverflow
Solution 2 - IosMelvinView Answer on Stackoverflow
Solution 3 - IosBergasmsView Answer on Stackoverflow
Solution 4 - IosDominic SanderView Answer on Stackoverflow
Solution 5 - IosVeeru SharmaView Answer on Stackoverflow
Solution 6 - IosSargisView Answer on Stackoverflow
Solution 7 - IosSureshkumar LinganathanView Answer on Stackoverflow