1.UDID

UDID (Unique Device Identifier)

Is a string of 40 hexadecimal numbers used to identify a unique device. It is now impossible to obtain this from code. If you want to see the UDID of your device, you can view it from iTunes.

  • Since iOS5, apple has removed access to the UDID through code, so it’s impossible for a coder to know the UDID of a user’s device.
  • Udids are not unique to jailbroken devices. Using the Cydia plugin UDIDFaker, you can assign a different UDID to each application. So the use of the UDID as a unique device for identification has become limited.

Code for getting UDID (deprecated after iOS5 and banned from the AppStore)

[[UIDevice currentDevice] uniqueIdentifier];

2.UUID

Universally Unique IDentifier (UUID)

Is based on a single application on an iOS device. As long as the user does not completely delete the application, the UUID remains the same for as long as the user uses the application. If the user deleted the application and then re-installed it, the UUID has changed.

  • Uuids of different applications on the same device are mutually exclusive, that is, applications can be identified on different devices. Therefore, some speculate that the UUID should be encrypted to generate a unique identifier based on the device identifier and application identifier (pure speculation).
  • The official recommended approach is to create a UUID within each application as a unique identifier and store it, but this solution is clearly unacceptable!
  • The UUID you create is different each time, which means that when you uninstall and reinstall the software, the UUID generated will not be the same as the unique identifier we require for data analysis.

The code for getting the UUID:

[[UIDevice currentDevice] identifierForVendor]; However, the device-specific issue remains: if you delete the app and install it again, the value of identifierForVendor changes.

2. Obtain the recommended new solution of the unique identifier of the device

Train of thought

  • The machine unique identifier UUID is generated by calling the CFFUUIDCreate function. However, each call to this function returns a different string, so the string needs to be stored after the first call.
  • Although the CFFUUIDCreate gets a different UUID each time, ideally it can be stored in the keychain and used as a unique identifier to identify the user’s device.

2.1 Official scheme for obtaining UUID

- (NSString *) uniqueString
{
   CFUUIDRef unique = CFUUIDCreate(kCFAllocatorDefault);
   NSString *result = [(NSString *)CFUUIDCreateString(kCFAllocatorDefault, unique) autorelease];
   CFRelease(unique);
   return result;
}
Copy the code

2.2 Unique Identifier Scheme based on SSKeychain

The UUID obtained above is based on a third-party library SSKeychain on Git, which can store the UUID in the keychain. Check whether there is a key string in each call. If there is a key string, use it; if there is no key string, write it in to ensure its uniqueness.

Reference code:

- (NSString *)getNewUniqueIdNum{
    
    NSString *uuidStr = [SSKeychain passwordForService:@"com.test.app1" account:@"user"];
    if(! uuidStr || [uuidStr isEqualToString:@""])
    {
        CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
        uuidStr = (__bridge NSString *)CFUUIDCreateString(kCFAllocatorDefault ,uuidRef);
        [SSKeychain setPassword:[NSString stringWithFormat:@"% @", uuidStr] forService:@"com.test.app1"account:@"user"];
    }
    return uuidStr;
}
Copy the code

Instead of the string conversion called above (__bridge NSString *), another way to write it is:

- (NSString *)getNewUniqueIdNum{
    
    NSString *identifierNumber = [SSKeychain passwordForService:@"com.test.app1"account:@"user"]; CFUUIDRef uuidRef = CFUUIDCreate(NULL); assert(uuidRef ! = NULL); CFStringRef uuidStrRef = CFUUIDCreateString(NULL, uuidRef);if(! identifierNumber){ [SSKeychainsetPassword: [NSString stringWithFormat:@"% @", uuidStrRef] forService:@"com.test.app1"account:@"user"];
        identifierNumber = [SSKeychain passwordForService:@"com.test.app1"account:@"user"];
    }
    return identifierNumber;
}
Copy the code

2.3 Other Schemes

Different from SSKeychain, based on a third party library SAMKeyChains. SAMKeyChains is a simple encapsulation of Apple security framework APIS, supporting access to passwords and accounts stored in keychains, including reading, deleting and setting. SAMKeyChains is simple to use and can be mastered by example code.

SAMKeyChains based on the reference code:

+ (NSString *)getDeviceId
{
    NSString * currentDeviceUUIDStr = [SAMKeychain passwordForService:@""account:@"uuid"];
    if (currentDeviceUUIDStr == nil || [currentDeviceUUIDStr isEqualToString:@""])
    {
        NSUUID * currentDeviceUUID  =[[UIDevice currentDevice] identifierForVendor];
        currentDeviceUUIDStr = [currentDeviceUUID UUIDString];
        currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""];
        currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString];
        [SAMKeychain setPassword: currentDeviceUUIDStr forService:@""account:@"uuid"];
    }
    return currentDeviceUUIDStr;
}
Copy the code

See SAMKeyChains Documentation for more detailed usage of SAMKeyChains

Reprinted from: unique Identifiers for iOS Devices About UDID alternatives: new method for generating unique identifiers based on UUID and SSKeychain