iPhone / iOS
Modern iOS Development: While Objective-C still works, Apple now recommends Swift for new iOS projects. See the Swift example below.
Objective-C (Legacy)
+ (NSString *)GetUUID
{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return [(NSString *)string autorelease];
}
Modern Objective-C (iOS 6.0+)
+ (NSString *)generateUUID
{
return [[NSUUID UUID] UUIDString];
}
// Usage:
NSString *uuid = [[NSUUID UUID] UUIDString];
NSLog(@"UUID: %@", uuid);
Swift (Recommended for New iOS Projects)
// Simple one-liner
let uuid = UUID().uuidString
print("UUID: \(uuid)")
// Or as a function
func generateUUID() -> String {
return UUID().uuidString
}
// Usage in a class
class MyClass {
let uniqueID = UUID().uuidString
}
source from stackoverflow.com