- (NSString *)getUniqueStrByUUID{
CFUUIDRef uuidObj = CFUUIDCreate (nil); // create a new UUID
// Get the string representation of the UUID
NSString * uuidString = (_ bridge_transfer NSString *) CFUUIDCreateString (nil, uuidObj );
CFRelease (uuidObj );
Return uuidString;
}
Here is a more detailed introduction to http://www.cocoachina.com/applenews/devnews/2013/0422/6040.html
In March 21, 2013, Apple notified developers that, starting from May 1, 2013,
Applications accessing the UIDID will no longer pass the reviewThe alternative solution is that developers should use the "Vendor or Advertising identifier introduced in iOS 6 ".
Unique Identifier is about to exit. Apple gave us two options: vender and Advertising identifier. But which one should be used? The document does not provide the exact answer. The specific use of this document depends entirely on the purpose of your app. Next, I will list the currently supported and discarded Unique Identifier methods in iOS, and explain them accordingly, hoping to help you make a correct decision.
CFUUID
CFUUID has appeared since iOS2.0. It is part of the CoreFoundatio package, so the API belongs to the C language style.CFUUIDCreateThe method is used to create cfuidref and obtain an NSString. The following code is used:
CFUUIDRef cfuuid = CFUUIDCreate (kCFAllocatorDefault); NSString * cfuidstring = (NSString *) CFBridgingRelease (cfuidcreatestring (kCFAllocatorDefault, cfuuid ));
The obtained CFUUID value system is not stored. Each time CFUUIDCreate is called, The system returns a new unique identifier. If you want to store this identifier, you need to store it in NSUserDefaults, Keychain, Pasteboard, or other places.
NSUUID
NSUUIDIt only appears in iOS 6, which is almost the same as CFUUID, except that it is an Objective-C interface. + (Id) UUID is a class method. You can call this method to obtain a UUID. The following code obtains a UUID string:
NSString * uuid = [[nsuuid uuid] UUIDString];
Like CFUUID, this value system won't store it, and a new unique identifier will be obtained each time it is called. If you want to store it, you need to store it yourself. When I read NSUUID, I noticed that the obtained value is exactly the same as that of CFUUID (but it may also be different ):
Example: 68753A44-4D6F-1226-9C60-0050E4C00067
Ad identifier (IDFA-identifierForIdentifier)
This is another new method in iOS 6,AdvertisingIdentifierIs part of the new framework AdSupport. framework. The ASIdentifierManager Singleton provides the advertisingIdentifier method. By calling this method, a NSUUID instance mentioned above is returned.
NSString * adId = [[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
Unlike CFUUID and NSUUID, AD identifiers are stored by the system. However, even if this is stored by the system, the ad identifier is re-generated in several cases. If the user completely resets the system (set the program-> General-> restore location and privacy), the AD identifier will be generated again. In addition, if the user explicitly restores the advertisement (set the program-> General-> about the local machine-> advertisement-> restores the advertisement identifier), the advertisement identifier will be generated again. For how to restore the ad identifier, note that if the program runs in the background, the user "restores the ad identifier" and then returns to the program, at this time, the obtained ad identifier does not immediately obtain the restored identifier. You must terminate the program and restart the program to obtain the restored ad identifier. I guess this is because ASIdentifierManager is a singleton.
The user has a controllable switch "restrict ad tracking" for the ad identifier ".In Nick shorttt's articleIt has been pointed out. Turn this switch on, but nothing is actually done, but this is to restrict your access to the ad identifier. This switch is a simple boolean flag. When you send an advertisement identifier to any server, you 'd better judge the value before making a decision.
Example: 1E2DFA89-496A-47FD-9941-DF1FC4E6484A
Vindor identifier (IDFV-identifierForVendor)
This method is also added in iOS 6, but the new method for obtaining this IDFV is added to the existing UIDevice class. Like advertisingIdentifier, this method returns an NSUUID object.
NSString * idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
In Apple's official document, identifierForVendor is described as follows:
The value of this property is the same for apps that come from the same vendor running on the same device. A different value is returned for apps on the same device that come from different vendors, and for apps on different devices regardless of vendor.
If such a condition is met, the obtained property value will not change: the same program-the same vindor-the same device. In this case, the value will not be the same: the same program-the same device-different vindor, or the same program-different devices-whether or not the same vindor.
After reading the above content, I have such a question: "What is vendor ". The first thing I think of is the apple developer account. But it turns out this is wrong. Next, I think there may be an AppIdentifierPrefix, which can be shared among multiple programs like key string access. This idea is also true. Finally, it is proved that the vendor is very simple: one is the first two parts of the CFBundleIdentifier (reverse DNS format. For example, the identifierForVendor obtained from com. doubleencore. app1 and com. doubleencore. app2 are the same, because the first two parts of their CFBundleIdentifier are the same. However, the identifierForVendor obtained in this way is completely different: com. massivelyoverrated or net. doubleencore.
Note that if you Uninstall all the programs corresponding to the same event and reinstall the programs provided by the same event, the identifierForVendor will be reset.
Example: 599F9C00-92DC-4B5C-9464-7971F01F8370
UDID
It is available in previous versions, but in iOS5 and later versions, and is discarded. Although this UDID is widely used, it is far away from developers and cannot be considered. It remains to be observed that the identifier is converted to a private method, or removed from the later iOS version. However, this UDID is very convenient when deploying an enterprise-level signature program. The method for obtaining a UDID is as follows:
NSString * udid = [[UIDevice currentDevice] uniqueIdentifier];
Example: bb4d786633053a0b9c0da20d54ea7e38e8776da4
OpenUDID
When iOS 5 was released, uniqueIdentifier was discarded, which caused developers to find a solution that can replace UDID and is not controlled by Apple. ThereforeOpenUDIDIt became the most widely used open-source UDID alternative solution at that time. OpenUDID is easy to implement in the project and supports a series of advertising providers.
NSString * openUDID = [OpenUDID value];
OpenUDID uses a clever method to store the identifier between different programs-a special name is used in the clipboard to store the identifier. In this way, other programs (also using OpenUDID) know where to get the generated identifier (instead of generating a new identifier ).
As mentioned earlier, Apple will force advertisingIdentifier or identifierForVendor in the future. If this day comes, even if OpenUDID seems to be a good choice, you may have to transition to the method launched by Apple.
Example: 0d944256b24c85900c764dd9f75ce054dc5986ff
Summary
We hope that the above information will help you select the correct unique identifier in the program. Here, I created a smallUnique Identifier Test ProgramYou can run the program and view the displayed content (including all the identifiers mentioned above ). In addition, the following two tables describe the availability in iOS and when to obtain the reset identifier.
* The program must be restarted to see the effect of the change.
** You can only view the changed values after deleting all the programs provided by the same vendor.
Source: ship-breaking blog double