首页 > 移动平台 > 详细

苹果禁用UUID了,咋办?

时间:2014-11-24 17:03:22      阅读:374      评论:0      收藏:0      [点我收藏+]

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:

苹果禁用UUID了,咋办?

原文:http://www.cnblogs.com/sunshine-anycall/p/4118936.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!