如何使用iPhone SDK创建GUID / UUID [英] How to create a GUID/UUID using the iPhone SDK

查看:152
本文介绍了如何使用iPhone SDK创建GUID / UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

推荐答案

[[UIDevice currentDevice] uniqueIdentifier]

返回iPhone的唯一ID。

Returns the Unique ID of your iPhone.


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

-[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,只需使用此方法(使用ARC):

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;
}

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

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

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

这篇关于如何使用iPhone SDK创建GUID / UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆