IOS gets the uniqueness of the device

Source: Internet
Author: User
Tags uuid reverse dns

One:

English Original: In iOS 7 and later, if you ask for the MAC address of an IOS device, the system returns the value 02:00:00:00:00:00. If you need to identify the device, use the Identifierforvendor property of Uidevice instead. (Apps that need a identifier for their own advertising purposes should consider using the Advertisingidentifier property of Asidentifiermanager instead.)
Translation: From IOS7 and later, if you request a MAC address from your iOS device, the system will return a fixed value of "02:00:00:00:00:00" if you need to identify the uniqueness of the device, Please use the Uidevice Identifierforvendor property. (If you need to identify your device for advertising purposes, consider using Asidentifiermanager's Advertisingidentifier property as an alternative)


What does this MAC address mean? What's the use?
MAC (Medium/media Access Control) address, which represents the identifier of each site on the internet, in hexadecimal notation, a total of six bytes (48 bits). Of these, the first three bytes are the code (high 24 bits) assigned by the registration Authority RA of the IEEE to different manufacturers, also known as the "unique identifier" (organizationally unique Identifier), and the last three bytes (low 24-bit) Adapter interfaces that are assigned to production by each manufacturer are called extension identifiers (uniqueness).
MAC address is used on the network to differentiate the uniqueness of the device, access to network devices have a MAC address, they must be different, is unique. There may be multiple MAC addresses on an iphone, including WiFi, SIM, and so on, but itouch and ipad have a WiFi connection, so just get the WiFi MAC address, which is En0 's address.
The image says that the MAC address is just like the ID number on our ID card and has global uniqueness. This can be very good to identify the device uniqueness, similar to the udid number of Apple devices, the usual use is: 1 for some statistical and analytical purposes, using the user's operating habits and data to better plan products; 2) as a user ID to uniquely identify the user, You can use the app as a visitor and save the appropriate information on the server side, eliminating the registration process such as user name, password, etc.


So, how do you use a MAC address to generate a device's unique identity? The main points are three kinds:
1. Direct use of "MAC Address"
2, use "MD5 (MAC Address)"
3, use "MD5 (Mac address+bundle_id)" To obtain "Machine + app" Unique identification (BUNDLE_ID is the unique identity of the app)


Before IOS7, because the MAC address is unique, the General app developer takes a 3rd way to identify the device that installs the corresponding app. Why would you use it? Before IOS5, both were used Udid and were later disabled. The use of UUID is recommended by Apple, but there are many problems with MAC addresses. and MAC address like Udid, there is a privacy issue, now Apple new release iOS7, if the request MAC address will return a fixed value, then Mac address+bundle_id This value everyone's device has become consistent, as the same as Udid is disabled. So, how to identify the device is unique?


There are many ways to get the unique identity of a device on an iOS system:
I. UDID (Unique Device Identifier)


Two. UUID (universally Unique Identifier)


Three. MAC Address


Four. OPEN UDID


Five. Advertising identifiers (Idfa-identifierforidentifier)


Six. Vindor identifiers (Idfv-identifierforvendor)


Seven. Push token+bundle_id

Udid's full name is unique device Identifier, which is the unique identifier of the Apple iOS device, which consists of 40 characters of letters and numbers (jailbroken devices can change the device's Udid by some tools). Mobile networks can use UDID to identify mobile devices, but since IOS5.0 (August 2011), Apple has announced that it will no longer support the use of the uniqueidentifier method to obtain the udid,ios5 of the device below. On March 21, 2013 Apple has informed developers that, from May 1, 2013 onwards, the program to access Uidids will no longer be approved, and the alternative is that developers should use the "vendor or advertising identifiers described in iOS 6". So Udid is absolutely useless.

OPEN UDID, does not use the MAC address, but also can ensure that different applications on the same device use the same openudid, as long as the user device has a use of openudid application exists, other subsequent installation of the application if the acquisition Openudid, Will get the one that was generated by the first app. However, according to the code and methods of the contributors, and some developers experience, if the use of the Openudid scheme of applications are all deleted, and then regain Openudid, at this time the Openudid is not the same as before. Visible, this method is still not insurance.

The ad identifier, another new method in iOS 6, provides a method for Advertisingidentifier, which returns a Nsuuid instance by calling the method, and finally obtains a UUID that is stored by the system. However, even though this is stored by the system, there are several cases where the ad identifier is regenerated. This ad identifier is regenerated if the user completely resets the system (restore location and privacy, general-purpose, Setup, and so on). In addition, if the user explicitly restores the ads (set-up, general-purpose, and so on, to the ads, and restore the ad identifiers), then the ad identifiers will be regenerated. With respect to the restoration of the ad identifier, it is important to note that if the program is running in the background, the user "restores the ad identifier" and then goes back to the program, and then gets the ad identifier and does not immediately get the restored identifier. You must terminate the program before restarting the program to obtain the restored AD identifier.

The Vindor identifier, also added in iOS 6, is the same as Advertisingidentifier, which returns a Nsuuid object that can get a UUID. If the condition "same program inside-same vindor-same device" is met, then the property value obtained will not change. This value will not be the same if it is "the same program-the same device-different vindor, or the same program-different devices-whether or not the same Vindor" is the case.


Ways to push token+bundle_id:
1, the application to increase the push to obtain tokens
2. Get Application bundle_id
3. Hash operation according to TOKEN+BUNDLE_ID


Apple push token guarantees that the device is unique, but must have a network condition to work, which is not dependent on the device itself, but relies on Apple push, and apple push sometimes gets out of the way.

The


UUID is an abbreviation for the universally unique identifier, which is a generic unique identifier in Chinese. It is to allow all the elements in the distributed system to have unique identification information, and do not need to through the central control side to do the identification information designation. In this way, everyone can create a UUID that does not clash with others. In this case, there is no need to consider the name duplication problem when the database is established. Apple recommends using a UUID to generate a unique identification string for your app.

The code for getting the UUID in iOS is as follows:-(nsstring*) uuid {cfuuidref puuid = cfuuidcreate (nil);     Cfstringref uuidstring = cfuuidcreatestring (nil, puuid);     NSString * result = (NSString *) cfstringcreatecopy (NULL, uuidstring);     Cfrelease (PUUID);     Cfrelease (uuidstring); return [result Autorelease]; The code for getting the NIC Mac in}ios is as follows: #include <sys/socket.h>//per msqr#include <sys/sysctl.h> #include <net/if.h># Include <net/if_dl.h> #pragma mark MAC addy//Return The local MAC addy//courtesy of FreeBSD hackers email list//A Ccidentally munged during previous update.    Fixed thanks to mlamb.-(NSString *) macaddress{int mib[6];    size_t Len;    Char *buf;    unsigned char *ptr;    struct IF_MSGHDR *IFM;       struct SOCKADDR_DL *sdl;    Mib[0] = ctl_net;    MIB[1] = Af_route;    MIB[2] = 0;    MIB[3] = Af_link;       MIB[4] = net_rt_iflist; if ((mib[5] = If_nametoindex ("en0") = = = 0) {printf ("Error:if_nametOindex error/n ");    return NULL;        } if (Sysctl (MIB, 6, NULL, &len, NULL, 0) < 0) {printf ("Error:sysctl, Take 1/n");    return NULL; } if ((Buf = malloc (len)) = = NULL) {printf ("Could not allocate memory.        error!/n ");    return NULL;        } if (Sysctl (MIB, 6, buf, &len, NULL, 0) < 0) {printf ("Error:sysctl, take 2");    return NULL;    IFM = (struct IF_MSGHDR *) buf;    SDL = (struct SOCKADDR_DL *) (IFM + 1);    ptr = (unsigned char *) lladdr (SDL); NSString *outstring = [NSString stringwithformat:@ "%02x:%02x:%02x:%02x:%02x:%02x", *ptr, * (ptr+1), * (ptr+2), * (ptr+3    ), * (PTR+4), * (ptr+5)]; NSString *outstring = [NSString stringwithformat:@ "%02x%02x%02x%02x%02x%02x", *ptr, * (ptr+1), * (ptr+2), * (ptr+3), * (PTR    +4), * (ptr+5)];    Free (BUF);  return [outstring uppercasestring];}

The developer can call once at the first launch of the app and then store the string for later use instead of Udid. However, if the user deletes the app and then installs again, a new string is generated, so it is not guaranteed to uniquely identify the device. This requires all the experts to come up with a variety of solutions. So, many applications have been using Mac Address before. But now if users upgrade to IOS7 (and later Apple systems), their MAC address is the same, there is no way to differentiate, only discard this method, re-use the UUID to identify. If you use the UUID, consider the processing when the app is removed and then reinstalled.

One solution: The UUID is usually generated only once, stored in the iOS system, if the app is deleted, after reloading the app its UUID is the same, unless the system resets. However, there is no guarantee that the system will be available after the upgrade (if the system saves the information).

Since the data stored in the iOS system is in the sandbox, the deletion of App,sandbox is no longer present. Fortunately there is an exception, that is keychain (keychain).


Typically, iOS systems use Nsuserdefaults to store data, but for some private information, such as passwords, certificates, and so on, you need to use a more secure keychain.


The information stored in the keychain is not lost due to the app being deleted. Therefore, this keychain feature can be used to preserve the unique identity of the device.


So, how to use keychain in the application, we need to import security.framework, Keychain operation Interface declaration in the header file SecItem.h.


Directly using the SecItem.h method to operate the keychain, the code needs to write more complex, we can use the already encapsulated tool class Keychainitemwrapper to operate on keychain.


Keychainitemwrapper is the Apple official example "Generickeychain" in a keychain common operation of the package class, after downloading the Generickeychain project on the official website,


Just copy the "KeychainItemWrapper.h" and "KEYCHAINITEMWRAPPER.M" to our project and import the Security.framework. Usage of Keychainitemwrapper:


/** Initializes a keychainitemwrapper to save the user account */
Keychainitemwrapper *wrapper = [[Keychainitemwrapper alloc] initwithidentifier:@ "account Number"
accessgroup:@ "YOUR_APP_ID_HERE.com.yourcompany.AppIdentifier"];

Save data
[Wrapper setobject:@] < account > "Forkey: (ID) ksecattraccount];

[Wrapper setobject:@ "< account password >" Forkey: (ID) ksecvaluedata];

Remove account password from Keychain
NSString *password = [wrapper objectforkey: (ID) ksecvaluedata];

Clear settings
[Wrapper Resetkeychainitem];
Where method "-(void) SetObject: (ID) inobject Forkey: (ID) key;" The value of the parameter "Forkey" should be the key that is defined in the file "SecItem.h" in Security.framework, and the key program with other strings will be wrong!

Two:

Cfuuid from iOS2.0, Cfuuid has appeared. It is part of the Corefoundatio package, so the API belongs to the C language style. The Cfuuidcreate method is used to create the cfuuidref, and a corresponding nsstring can be obtained, as in the following code:



NSString *cfuuidstring = (nsstring*) cfbridgingrelease (cfuuidcreatestring (Kcfallocatordefault, Cfuuid));
Copy Code

The system is not stored for this cfuuid value. Each time you call Cfuuidcreate, the system returns a new unique identifier. If you want to store this identifier, you need to store it yourself in Nsuserdefaults, Keychain, pasteboard, or somewhere else.

Example: 68753a44-4d6f-1226-9c60-0050e4c00067

Nsuuidnsuuid only appears in iOS 6, which is almost exactly the same as cfuuid, except that it is a objective-c interface. The + (ID) UUID is a class method that calls the method to obtain a UUID. A UUID string can be obtained by using the following code:

NSString *uuid =[[nsuuid uuid] uuidstring];
Copy Code

Like Cfuuid, this value system is not stored and a new unique identifier is obtained each time it is called. If you want to store it, you need to store it yourself. When I read the nsuuid, I noticed that the value obtained was exactly the same as cfuuid (though it might be different):

Example: 68753a44-4d6f-1226-9c60-0050e4c00067

Three:

Vendor is the first two parts of cfbundleidentifier (reverse DNS format). Applications from the same carrier run on the same device, the value of this property is the same, and different carrier applications run on the same device with different values.

After testing, as long as there is a Tencent app on the device, reinstall the Identifierforvendor value unchanged, if the Tencent app is all removed, reinstall the Identifierforvendor value change.

IOS gets the uniqueness of the device

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.