UIDevice uniqueIdentifier deprecated - What to do now?

后端 未结 30 2231
日久生厌
日久生厌 2020-11-21 06:07

It has just come to light that the UIDevice uniqueIdentifier property is deprecated in iOS 5 and unavailable in iOS 7 and above. No alternative method or pr

30条回答
  •  别那么骄傲
    2020-11-21 06:52

    Create your own UUID and then store it in the Keychain. Thus it persists even when your app gets uninstalled. In many cases it also persists even if the user migrates between devices (e.g. full backup and restore to another device).

    Effectively it becomes a unique user identifier as far as you're concerned. (even better than device identifier).

    Example:

    I am defining a custom method for creating a UUID as :

    - (NSString *)createNewUUID 
    {
        CFUUIDRef theUUID = CFUUIDCreate(NULL);
        CFStringRef string = CFUUIDCreateString(NULL, theUUID);
        CFRelease(theUUID);
        return [(NSString *)string autorelease];
    }
    

    You can then store it in KEYCHAIN on the very first launch of your app. So that after first launch, we can simply use it from keychain, no need to regenerate it. The main reason for using Keychain to store is: When you set the UUID to the Keychain, it will persist even if the user completely uninstalls the App and then installs it again. . So, this is the permanent way of storing it, which means the key will be unique all the way.

         #import "SSKeychain.h"
         #import 
    

    On applictaion launch include the following code :

     // getting the unique key (if present ) from keychain , assuming "your app identifier" as a key
           NSString *retrieveuuid = [SSKeychain passwordForService:@"your app identifier" account:@"user"];
          if (retrieveuuid == nil) { // if this is the first time app lunching , create key for device
            NSString *uuid  = [self createNewUUID];
    // save newly created key to Keychain
            [SSKeychain setPassword:uuid forService:@"your app identifier" account:@"user"];
    // this is the one time process
    }
    

    Download SSKeychain.m and .h file from sskeychain and Drag SSKeychain.m and .h file to your project and add "Security.framework" to your project. To use UUID afterwards simply use :

    NSString *retrieveuuid = [SSKeychain passwordForService:@"your app identifier" account:@"user"];
    

提交回复
热议问题