在Android中,Advertising ID是用Java语言实现的,使用RAND_UUID算法生成唯一标识符,代码示例如下:
import java.util.UUID;
public class AdvertisingIdClient {
public static String getAdvertisingId(Context context) throws Exception {
ContentResolver cr = context.getContentResolver();
String advertisingId = Settings.Secure.getString(cr, Settings.Secure.ANDROID_ID);
if (advertisingId == null || advertisingId.equals("9774d56d682e549c")) { // "9774d56d682e549c"是模拟器默认的ID
advertisingId = UUID.randomUUID().toString();
}
return advertisingId;
}
}
在iOS中,Advertising ID也是基于UUID算法生成唯一标识符,使用CFUUIDRef库实现,代码示例如下:
#import
NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
需要注意的是,在iOS 14及以上系统版本中,为了保护用户隐私,需要先获取AppTrackingTransparency权限才能获取Advertising ID。具体代码示例可参考苹果官方文档。