如何解决苹果公司禁止用UUID的方法。

转载 2012年03月30日 10:42:59

The UDID Apocalypse or: How I Learned To Stop Using NSUserDefaults And Love The Keychain

By now you have probably heard that Apple is deprecating support for attaining a UDID from an iOS device and furthermore that they will be rejecting any app submissions that use the UDID in any way. The now deprecated way of retrieving the UDID was:

NSString *udid = [[UIDevice currentDevice] identifier];

As we can no longer use this, but will often still have need of a unique identifier Apple has suggested using a CFUUID. To get a unique string identifier you now need to do this:

CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
NSString *uuid = (NSString *)CFUUIDCreateString (kCFAllocatorDefault,uuidRef);

This gives you a unique identifier however if you called these methods again you would get a different unique identifier which may be fine if you only ever need to use this identifier once but for many situations this is probably not what you want. Apple suggests using NSUserDefaults to store the UUID after you have made it. You would do that like so:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:uuid forKey:@"UUID"];

Then if you need to retrieve the UUID at any point you would call:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *uuid = [userDefaults objectForKey:@"UUID"];

This is an ok solution. I say it is only ok because the problem with NSUserDefaults is that they do not persist if the user removes and reinstalls their application. This could wreak havoc on your app if for example the UUID is used to identify the device to a web service that served data to your app. Your users would find it frustrating to lose their data simply because they had reinstalled your app.

A better solution is to store the UUID in the users keychain.

If you are unfamiliar with the concept it is fairly simple. Each app has its own keychain that can be used to securely store passwords, certificates, keys etc. The keychain can even be shared among several different apps if needed though I will not cover that today.

To make working with the keychain simpler Apple wrote an Objective-C wrapper class called KeychainItemWrapper which you can find here.

To store our UUID in the keychain we first create a KeychainItemWrapper object with:

KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"UUID" accessGroup:nil];

You can use whatever you want for the identifier. As I am only making this item available to this app I set the accessGroup to nil.

Now to save your UUID to the keychain use:

[keychainItem setObject:uuid forKey:(id)kUUID];

In this case I would have defined the constant kUUID with a # define such as:

#define kUUID @"UUID"

To retrieve your UUID you need to make a keychainItemWrapper object and then call objectForKey like this:

KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"UUID" accessGroup:nil];
[keychainItem objectForKey:(id)kUUID];

This UUID that is stored in the keychain will now persist if the user removes the app and reinstalls and even if they save an encrypted backup and do a restore. It will not persist if they do a full reset of the device or restore from an unencrypted backup.

One word of warning, the keychain does not work in the iOS simulator.

Share and Enjoy:


10个窍门:怎么使用公司禁用软件?

10个窍门:怎么使用公司禁用软件? ·        为了查明是否能够信任IT部门,我们向网络专家寻求一些建议。很清晰地,我们让专家找出我们的IT部门不会告知我们的10个秘密。比如如何不留踪迹的浏览...
  • maochengtao
  • maochengtao
  • 2013年04月23日 16:45
  • 2738

iOS 开发者中的公司账号与个人账号之间有什么区别?

作者:李宏涛 链接:http://www.zhihu.com/question/20308474/answer/73188972 来源:知乎 著作权归作者所有,转载请联系作者获得授权。 李宏涛...
  • qq_18608029
  • qq_18608029
  • 2016年07月21日 13:18
  • 3237

微信分享屏蔽跳转appstore解决方法

转自:http://blog.csdn.net/a416863220/article/details/46117053 XX的企鹅,什么尿性大家都知道。 最近就是碰到分享的东西到朋友圈...
  • gf771115
  • gf771115
  • 2015年12月29日 10:06
  • 11798

iOS—如何申请苹果公司开发者账号流程详细图文介绍(包括邓白氏编码的申请方法详细介绍)

iOS—如何申请苹果公司开发者账号流程详细图文介绍(包括邓白氏编码的申请方法详细介绍) 字数2206 阅读12673 评论28 喜欢21 这是我第一次申请苹果的公司开发者账号,走了很多...
  • qq_31292239
  • qq_31292239
  • 2016年09月05日 23:04
  • 249

关于苹果公司ipad等产品使用中国移动WLAN网络解决方案总结报告

  • 2014年05月08日 14:20
  • 3.54MB
  • 下载

GCD 苹果公司为多核的并行运算提供的解决方案

GCD:        GCD是苹果公司为多核的并行运算提出的解决方案        GCD会自动利用更多的CPU内核        GCD会自动管理线程的生命周期        程序员只需告...
  • ihy0506
  • ihy0506
  • 2015年11月22日 14:21
  • 541

苹果公司6月1日后发布的应用必须支持IPv6-Only网络的解决办法(底层socket连接的IPv6支持方案)

苹果在去年的WWDC2015中宣布在今年的6月1日之后发布的应用必须支持IPv6-Only的网络环境。 苹果官方给出的解决方法是: IP address literals embe...
  • Michaelin1208
  • Michaelin1208
  • 2016年06月13日 20:28
  • 2524

管理人 -- 苹果公司资深主管项目经验谈

  • 2008年07月19日 10:02
  • 899KB
  • 下载

浅谈苹果公司的创新模式

  • 2015年01月19日 14:06
  • 28KB
  • 下载

苹果公司展示的ppt

  • 2015年03月15日 14:49
  • 691KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何解决苹果公司禁止用UUID的方法。
举报原因:
原因补充:

(最多只允许输入30个字)