如何使用iOS创建GUID / UUID

How to create a GUID/UUID using iOS

我希望能够在iPhone和iPad上创建一个guid/uuid。

其目的是能够为所有唯一的分布式数据创建密钥。有没有一种方法可以通过iOS SDK做到这一点?


1
[[UIDevice currentDevice] uniqueIdentifier]

独特的ID of the归来你的iPhone。P></

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.

如果你创建的UUID just need to several method(with this,使用ARC):P></

1
2
3
4
5
6
7
+ (NSString *)GetUUID
{
  CFUUIDRef theUUID = CFUUIDCreate(NULL);
  CFStringRef string = CFUUIDCreateString(NULL, theUUID);
  CFRelease(theUUID);
  return (__bridge NSString *)string;
}

编辑:2014年1月29日:如果你还针对前6后,现在你可以使用简单的内部法:茶多P></

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


在这里简单的AM is the using就绪队列,与弧。P></

1
2
3
4
5
6
7
8
+(NSString *)getUUID
{
    CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
    NSString * uuidString = (__bridge_transfer NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
    CFRelease(newUniqueId);

    return uuidString;
}


你可以在含6 easily使用:P></

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

更多的细节在Apple的文献P></


苹果reviewing developer found the documentation的对象cfuuid is available on the iPhone OS 2.0及以后。P></


在斯威夫特P></

1
2
var uuid: String = NSUUID().UUIDString
println("uuid: \(uuid)")

NSString *uuid = [[NSProcessInfo processInfo] globallyUniqueString]is to the扩技术的使用。see the nsprocessinfo类参考。P></


斯威夫特在3.0P></

1
var uuid = UUID().uuidString

但我的无知我uploaded快速执行objc here for of a class对象的GUID的GUID(C):P></

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

它可以找到各种字符串to and from formats为好。P></