How to get the unique identifier of the device in iOS

 

In the development process, the application needs to record the device identification, even if the application is uninstalled and then installed, it can be re-identified. Here is an implementation method - read the UUID (Universally Unique Identifier) ​​of the device and record it through KeyChain.

First of all, the method of obtaining the unique identifier of the device in iOS has been changing with the update of the version. After iOS 2.0, UIDevice provides a method uniqueIdentifier to obtain the unique identifier of the device. Through this method, we can obtain the serial number of the device, which is also the only identifier that can be confirmed so far. The good times did not last long, because the unique identifier corresponds to the mobile phone one-to-one, Apple felt that it might leak user privacy, so this method was abandoned after iOS 5.0; iOS 6.0 system added two new interfaces for replacing uniqueIdentifier , respectively: identifierForVendor, advertisingIdentifier, but these two interfaces will change the value when the app is reinstalled, and they are not the only identifiers, so the developer uses the WiFi mac address instead; Apple also blocked the mac address in iOS 7 , so the developer changed his mind again to use the KeyChain to save the obtained UDID, so that even if the APP is deleted and installed, it can be read back from the KeyChain.

First save the UUID of the device, you can use the class method + (id)UUID is a class method, call this method to get a UUID. A UUID string can be obtained by the following code:

 NSString *uuid = [[NSUUID UUID] UUIDString];

It is also possible to save the new Vindor identifier (IDFV-identifierForVendor) in iOS 6. A new method to obtain this IDFV is added to the existing UIDevice class. Like advertisingIdentifier, this method returns an NSUUID object.

NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

If the user uninstalls all the programs corresponding to the same vendor, and then reinstalls the programs provided by the same vendor, the identifierForVendor will be reset, so KeyChain is used here to save.
KeyChain (keychain) is often used with Apple devices. Usually, if you want to debug, you have to install certificates and the like. These certificates are stored in KeyChain, and the account passwords that we usually browse the web are also recorded in KeyChain. middle. Compared with OS X, the KeyChain in iOS is simpler. There is only one KeyChain in the whole system. Each program can record data in the KeyChain, and can only read the data recorded in the KeyChain by its own program. The Security.framework framework in iOS provides four main methods to manipulate KeyChain:

  • SecItemCopyMatching(CFDictionaryRef query, CFTypeRef *result);//查询OSStatus
  • SecItemAdd(CFDictionaryRef attributes, CFTypeRef *result); //添加OSStatus
  • SecItemUpdate(CFDictionaryRef query, CFDictionaryRef attributesToUpdate); //更新KeyChain中的ItemOSStatus
  • SecItemDelete(CFDictionaryRef query)//Delete ItemOSStatus in KeyChain

The parameters of these four methods are relatively complex, and once they are passed incorrectly, the operation of the KeyChain will fail. The details are described in the documentation. You can check the official documentation . The KeyChain provided by Apple is a little troublesome to use, so a third-party library SAMKeyChains is recommended here. SAMKeyChains simply encapsulates the Apple Security Framework API and supports access to passwords and accounts stored in the keychain, including reading, deleting and setting. . SAMKeyChains is easy to use and can be mastered through example code.

//保存一个UUID字符串到钥匙串:
CFUUIDRef uuid = CFUUIDCreate(NULL);
assert(uuid != NULL);
CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
 [SAMKeychain setPassword: [NSString stringWithFormat:@"%@", uuidStr]
 forService:@"com.yourapp.yourcompany"account:@"user"];
 
//从钥匙串读取UUID:
NSString *retrieveuuid = [SAMKeychain passwordForService:@"com.yourapp.yourcompany"account:@"user"];
 
**注意: setPassword和passwordForSevice方法中的**services 和 accounts 参数应该是一致的。

For more detailed usage instructions, see SAMKeyChains Documentation

The basic implementation idea is like this. The following is a specific implementation code for reference only.

+ (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;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324390988&siteId=291194637