During development, you will encounter situations where the application needs to record the device mark, and it can be re-identified even if the application is uninstalled and then installed. Here is a way to write an implementation - read the UUID of the device (Universally Unique Identifier) and record it through KeyChain.
First of all, the method of obtaining device unique identifiers in iOS continues to change with the update of the version. After iOS 2.0, UIDevice provides a uniqueIdentifier method to obtain the device's unique identifier. Through this method, we can obtain the device's serial number, which is also the only unique identifier that can be confirmed so far. The good times did not last long, because the unique identifier corresponds to the mobile phone one by one, Apple felt that it might leak user privacy, so the method was discarded after iOS 5.0; iOS 6.0 system has added two new interfaces to replace uniqueIdentifier, namely: identifierForVendor and advertisingIdentifier, but these two interfaces will change the value when the application is reinstalled, and are not the unique identifier, so the developer changed his mind to use WiFi's mac address to replace it; in iOS 7, Apple blocked the mac address, so the developer changed his mind and used KeyChain to save the obtained UDID, so that even if the APP is deleted and then installed, it can be read back from KeyChain.
First, save the UUID of the device. You can use the class method + (id)UUID to be a class method. Calling this method can obtain a UUID. You can get a UUID string through the following code:
NSString *uuid = [[NSUUID UUID] UUIDString];
You can also save the new Vindor identifier (IDFV-identifierForVendor) added in iOS 6. The 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 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 is often used on Apple devices. Usually, if you want to debug, you have to install certificates or the like. These certificates are saved in KeyChain, and the account passwords we usually browse web pages are also recorded in KeyChain. KeyChain in iOS is simpler than OS X. There is only one KeyChain in the entire system. Each program can record data in KeyChain, and can only read the data recorded in KeyChain by its own program. Frameworks in iOS provide four main ways to manipulate KeyChain:
- SecItemCopyMatching(CFDictionaryRef query, CFTypeRef *result);//Query OSStatus
- SecItemAdd(CFDictionaryRef attributes, CFTypeRef *result); //Add OSStatus
- SecItemUpdate(CFDictionaryRef query, CFDictionaryRef attributesToUpdate);//Update ItemOSStatus in KeyChain
- SecItemDelete(CFDictionaryRef query)//Delete ItemOSStatus in KeyChain
The parameters of these four methods are relatively complicated. Once the error passes, it will cause the operation of KeyChain to fail. The document is detailed, so you can check it out.Official Documentation. The KeyChain provided by Apple is a little troublesome to use, so here is a third-party library that simply encapsulates the Apple Security Framework API, supporting access to passwords and accounts stored in the keychain, including reading, deletion and setting. SAMKeyChains is easy to use and can be mastered through instance code.
//Save a UUID string to the keychain:CFUUIDRef uuid = CFUUIDCreate(NULL); assert(uuid != NULL); CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid); [SAMKeychain setPassword: [NSString stringWithFormat:@"%@", uuidStr] forService:@""account:@"user"]; //Read the UUID from the keychain:NSString *retrieveuuid = [SAMKeychain passwordForService:@""account:@"user"];
**Note: The **services and accounts parameters in the setPassword and passwordForSevice methods should be consistent.
More detailed usage instructions can be foundSAMKeyChains Documentation
This is the basic implementation idea. Below 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 = ; currentDeviceUUIDStr = [currentDeviceUUIDStr stringByReplacingOccurrencesOfString:@"-" withString:@""]; currentDeviceUUIDStr = [currentDeviceUUIDStr lowercaseString]; [SAMKeychain setPassword: currentDeviceUUIDStr forService:@" "account:@"uuid"]; } return currentDeviceUUIDStr; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more. ,