Objective-C
Modern iOS/macOS Development: While Objective-C still works, Apple now recommends Swift for new 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+ / macOS 10.8+)
+ (NSString *)generateUUID
{
return [[NSUUID UUID] UUIDString];
}
// Usage:
NSString *uuid = [[NSUUID UUID] UUIDString];
NSLog(@"UUID: %@", uuid);
Swift (Recommended for New Projects)
// Simple one-liner
let uuid = UUID().uuidString
print("UUID: \(uuid)")
// Or as a function
func generateUUID() -> String {
return UUID().uuidString
}
source from stackoverflow.com