idfa是App推广过程中识别用户的重要标识数据!在iOS 14 之后,关于idfa的获取发生了变化,将会对APP的推广产生较大的影响
今天我们从代码层次来进行探讨 关于idfa的变动
在iOS14 之前我们都是这样获取的
引入头文件
#import <AdSupport/AdSupport.h>
代码实现
if ([ASIdentifierManager.sharedManager isAdvertisingTrackingEnabled]) {
NSString * idfa = [ASIdentifierManager.sharedManager advertisingIdentifier].UUIDString;
}else {
NSLog(@"用户开启了限制广告追踪");
}
但是在iOS14中,获取我们还是通过同样的方法进行获取,但是我们需要先进行权限的申请(和使用定位相机一样),如果不申请权限,获取到的idfa 将会是 0000-0.......000
- 在info.plist 中添加 权限申请描述
<key>NSUserTrackingUsageDescription</key>
<string>请允许获取并使用您的IDFA</string>
- 进行权限判断,根据不同权限进行判断
if (@available(iOS 14, *)) {
ATTrackingManagerAuthorizationStatus status = ATTrackingManager.trackingAuthorizationStatus;
switch (status) {
case ATTrackingManagerAuthorizationStatusDenied:
NSLog(@"用户拒绝");
break;
case ATTrackingManagerAuthorizationStatusAuthorized:
NSLog(@"用户允许");
break;
case ATTrackingManagerAuthorizationStatusNotDetermined:
NSLog(@"用户为做选择或未弹窗");
break;
default:
break;
}
} else {
// Fallback on earlier versions
if ([ASIdentifierManager.sharedManager isAdvertisingTrackingEnabled]) {
}else {
NSLog(@"用户开启了限制广告追踪");
}
}
- 申请权限
if (@available(iOS 14, *)) {
[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
NSLog(@"app追踪权限:%lu",(unsigned long)status);
}];
} else {
// Fallback on earlier versions
}
- 获取idfa
NSString * idfa = [ASIdentifierManager.sharedManager advertisingIdentifier].UUIDString;
附件:
image image
网友评论