- Home /
deviceUniqueIdentifier for iOS changed since upgrading to 4.2
We upgraded our project to Unity 4.2 recently and noticed SystemInfo.deviceUniqueIdentifier is returning differently to older Unity versions in iOS devices.
Is this expected? Anything we can do to keep using the device id of old version?
Many thanks, Lu
Answer by luape · Aug 01, 2013 at 11:46 PM
Not sure if it is breaking the rule to answer my own question. But here goes:
Turns out the md5 function used in Unity 4.2 is not the same as previous versions. To keep old deviceUniqueIdentifier here are the changes:
In XCode project generated by Unity, find file called DeviceSettings.mm, inside _InitDeviceIDPreIOS7()
REPLACE:
unsigned char hash_buf[MD5_DIGEST_LENGTH];
CC_MD5(macaddr_str, sizeof(macaddr_str), hash_buf);
char uid_str[MD5_DIGEST_LENGTH*2 + 1] = {0};
for(int i = 0 ; i < MD5_DIGEST_LENGTH ; ++i)
::sprintf(uid_str + 2*i, "%02x", hash_buf[i]);
_DeviceID = [NSString stringWithUTF8String:uid_str];
WITH:
unsigned char result[16];
CC_MD5( macaddr_str, strlen(macaddr_str), result );
_DeviceID = [[NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
] retain];
Used retain or it will crash on later deviceUniqueIdentifier calls, which is another bug in Unity4.2. (md5 code from another stackoverflow post)
The problem with this change is that you will have to change on every developer machine as the code is part of Unity's application. For this reason, we made our own iOS plugin, which returns the same result. And we start calling that plugin instead of SystemInfo.deviceUniqueIdentifier. This way, the code is bound to our project and carries across to all developers checking out the repository.
You should also know that from iOS7, there is a possibility that MAC address may be blocked. When this happens, there is no way for current unique device id to work. You should plan for migration methods to (post ios7) deviceUniqueIdentifier method using ios vender id, etc.
Hope this helps.
It it not breaking a rule. In fact, you should mark it as correct using the currently gray tick mark.
Your answer
Follow this Question
Related Questions
" Prepare iOS For Recording " on iOS 1 Answer
Unity 4.2 IOS Exception Handling 0 Answers
Unity 4.2.1 iOS 8.2 Orientation issue 0 Answers
4.2 iOS Exceptions Not Printed 2 Answers
Submit to app store rejected for icon sizes and push notification 2 Answers