Does anyone know how to get the ID which AdMob needs to show test adverts on iOS? I thought that I’d be able to use iPhone.advertisingIdentifier, but that doesn’t seem to be the case.
This is what I use for Android, which works, so the md5 method seems fine:
In the end, the problem was not with the device ID was using, it was a setting within iOS. In order to retrieve the advertising identifier, the device must have “Limit Ad Tracking” turned OFF. This can be found in Settings > Privacy.
In case anyone wonders, these methods with return the device IDs in the format AdMob uses.
#if UNITY_ANDROID
public static string GetAndroidAdMobID() {
UnityEngine.AndroidJavaClass up = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer");
UnityEngine.AndroidJavaObject currentActivity = up.GetStatic<UnityEngine.AndroidJavaObject>("currentActivity");
UnityEngine.AndroidJavaObject contentResolver = currentActivity.Call<UnityEngine.AndroidJavaObject>("getContentResolver");
UnityEngine.AndroidJavaObject secure = new UnityEngine.AndroidJavaObject("android.provider.Settings$Secure");
string deviceID = secure.CallStatic<string>("getString", contentResolver, "android_id");
return Md5Sum(deviceID).ToUpper();
}
#endif
#if UNITY_IPHONE
public static string GetIOSAdMobID() {
return Md5Sum(UnityEngine.iPhone.advertisingIdentifier);
}
#endif
public static string Md5Sum(string strToEncrypt) {
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
string hashString = "";
for (int i = 0; i < hashBytes.Length; i++) {
hashString += System.Convert.ToString(hashBytes*, 16).PadLeft(2, '0');*