如何使用 iOS 创建 GUID/UUID

发布于 2024-07-11 16:25:55 字数 98 浏览 5 评论 0 原文

我希望能够在 iPhone 和 iPad 上创建 GUID/UUID。

目的是能够为分布式数据创建唯一的密钥。 有没有办法使用 iOS SDK 来做到这一点?

I want to be able to create a GUID/UUID on the iPhone and iPad.

The intention is to be able to create keys for distributed data that are all unique. Is there a way to do this with the iOS SDK?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

旧夏天 2024-07-18 16:25:55
[[UIDevice currentDevice] uniqueIdentifier]

返回您的 iPhone 的唯一 ID。

编辑:-[UIDevice uniqueIdentifier] 现已弃用,应用程序因使用它而被 App Store 拒绝。 下面的方法现在是首选方法。

如果您需要创建多个 UUID,只需使用此方法(使用 ARC):

+ (NSString *)GetUUID
{
  CFUUIDRef theUUID = CFUUIDCreate(NULL);
  CFStringRef string = CFUUIDCreateString(NULL, theUUID);
  CFRelease(theUUID);
  return (__bridge NSString *)string;
}

编辑:2014 年 1 月 29 日:
如果您的目标是 iOS 6 或更高版本,您现在可以使用更简单的方法:

NSString *UUID = [[NSUUID UUID] UUIDString];
[[UIDevice currentDevice] uniqueIdentifier]

Returns the Unique ID of your iPhone.

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.

If you need to create several UUID, just use this method (with ARC):

+ (NSString *)GetUUID
{
  CFUUIDRef theUUID = CFUUIDCreate(NULL);
  CFStringRef string = CFUUIDCreateString(NULL, theUUID);
  CFRelease(theUUID);
  return (__bridge NSString *)string;
}

EDIT: Jan, 29 2014:
If you're targeting iOS 6 or later, you can now use the much simpler method:

NSString *UUID = [[NSUUID UUID] UUIDString];
吻风 2024-07-18 16:25:55

这是我正在使用的简单代码,符合 ARC。

+(NSString *)getUUID
{
    CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
    NSString * uuidString = (__bridge_transfer NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
    CFRelease(newUniqueId);

    return uuidString;
}

Here is the simple code I am using, compliant with ARC.

+(NSString *)getUUID
{
    CFUUIDRef newUniqueId = CFUUIDCreate(kCFAllocatorDefault);
    NSString * uuidString = (__bridge_transfer NSString*)CFUUIDCreateString(kCFAllocatorDefault, newUniqueId);
    CFRelease(newUniqueId);

    return uuidString;
}
枉心 2024-07-18 16:25:55

在 iOS 6 中,您可以轻松使用:

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

Apple 文档

In iOS 6 you can easily use:

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

More details in Apple's Documentations

箜明 2024-07-18 16:25:55

查看Apple开发人员文档,我发现 CFUUID 对象在 iPhone OS 2.0 及更高版本上可用。

Reviewing the Apple Developer documentation I found the CFUUID object is available on the iPhone OS 2.0 and later.

埖埖迣鎅 2024-07-18 16:25:55

Swift中:

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

In Swift:

var uuid: String = NSUUID().UUIDString
println("uuid: \(uuid)")
沉睡月亮 2024-07-18 16:25:55

最简单的技术是使用 NSString *uuid = [[NSProcessInfo processInfo]globalUniqueString]。 请参阅NSProcessInfo 类引用。

The simplest technique is to use NSString *uuid = [[NSProcessInfo processInfo] globallyUniqueString]. See the NSProcessInfo class reference.

开始看清了 2024-07-18 16:25:55

Swift 3.0

var uuid = UUID().uuidString

In Swift 3.0

var uuid = UUID().uuidString
乖乖哒 2024-07-18 16:25:55

我在这里上传了简单但快速的 ObjC Guid 类实现:obj -c GUID

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

它也可以解析各种字符串格式。

I've uploaded my simple but fast implementation of a Guid class for ObjC here: obj-c GUID

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

It can parse to and from various string formats as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文