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:

最新文章

  1. Android利用Jsoup解析html 开发网站客户端小记。
  2. 浅谈JavaScript eval() 函数
  3. linux 获取线程号
  4. springboot使用之四:错误页面404处理建议
  5. 使用apache.lang包安全简洁地操作Java时间
  6. 【Python】一个简单的例子
  7. office2010 office2013打开个别PPT时需要修复的解决方法
  8. ExtJS4.2学习(13)基于表格的扩展插件---rowEditing
  9. [Drools]JAVA规则引擎 -- Drools
  10. 【转】Loss Function View
  11. Zoj 3842 Beauty of Array
  12. NSIS:判断程序是否运行并进行卸载
  13. css相关tips
  14. 使用Lucene.net+盘古分词实现搜索查询
  15. 关于9080端口和80端口实现真正意义的WebServer+ApplicationServer结合应用
  16. cpu-z笔记本加条子
  17. css 常用布局
  18. python---生成验证码图片
  19. js一次控制 多个style样式
  20. 纯css瀑布流布局

热门文章

  1. VMware ESXi NAT实现
  2. api proxy设置 后端服务器代理
  3. 启动zookeeper时,jps显示有进程,但是status查看状态时就Error contacting service. It is probably not running
  4. 汇编_指令_SHL、SHR、SAL、SAR、ROL、ROR、RCL、RCR
  5. windows8.1中组件服务DCOM配置里属性灰色不可修改的解决办法
  6. 0002_20190328_Centos修改系统时间
  7. Python开发一个堡垒机
  8. Nginx实战入门教程
  9. ES6系列_6之新增的数组知识
  10. 在C#里面获得应用程序的当前路径