Getting unique ID of non-phone iDevices and Android tablets
My app uses a device's phone number to get meaningful information about the user's contacts. It runs on Android and iOS.
On a phone, I simply use the 10-digit (no country code) phone number as the user's unique identifier. This even works on the Android simulator (which has its own defunct number), though the iPhone simulator returns a blank number (I can easily ignore this case).
Now I got to testing on non-phone devices. While they 开发者_开发百科still have contacts available, they no longer have a phone number. An easy approach would be to use the UDID in iOS and whatever the equivalent is in Android. However, I have 2 problems I need solving:
The UDID is not uniform. I need a 10 character key. Is there any way to hash n-characters into 10-characters, avoiding collisions? (I can live with very low probability collisions)
Even a bigger issue: I just read that Apple is blocking UDID access as of iOS 5. What shall I use in its stead, again, with the need to maintain a 10-character key?
Thanks for your time.
You can use the MAC address How can I programmatically get the MAC address of an iphone . There is a solution out there that creates a UUID from the MAC address and the bundle identifier of the app on github https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5 .
I think the MAC address on the iphone is adequate. Mac addresses have 12 chars, so you just need to decide on how you want to squish it to 10 chars.
You can use any hash method you wish; if you have a requirement that the result only be 10 characters, you can reduce a larger hash to 10 characters easily enough - you could clip the result at 10 characters, you could XOR the 11th through 20th with the 1st through 10th (cycling if your hash has more than 20 characters), etc.
The bigger challenge on Android is going to be finding a device ID which is unique -- there is a mechanism Google put in place, however it relies on OEM compliance and that compliance is not uniform across all devices. Is there a unique Android device ID? contains some good information though.
Check this out it might help. This is from Localytics Open Source SDK
/**
* Gets a 1-way hashed value of the device's unique ID. This value is
* encoded using a SHA-256 one way hash and cannot be used to determine what
* device this data came from.
*
* @param appContext
* The context used to access the settings resolver
* @return An 1-way hashed identifier unique to this device or null if an
* ID, or the hashing algorithm is not available.
*/
public static String getGlobalDeviceId(final Context appContext) {
String systemId = System.getString(appContext.getContentResolver(),
System.ANDROID_ID);
if (systemId == null) {
return null;
}
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] digest = md.digest(systemId.getBytes());
BigInteger hashedNumber = new BigInteger(1, digest);
return new String(hashedNumber.toString(16));
} catch (NoSuchAlgorithmException e) {
return null;
}
}
精彩评论