iOS - IDFA Permission AppTrackingTransparency

For iOS14 and above systems, the method of obtaining IDFA has changed.
Apple notifies
the App that it needs to obtain the user's permission through AppTrackingTransparency in order to track the user and access the advertising identifier of their device. Similar to cameras, photo albums, maps, etc., a user authorization box will pop up, as shown below. Introducing ATT settings.

1. Turn on the main switch of app tracking in privacy, otherwise the pop-up box will not work. If the switch is
turned on and the pop-up box still cannot be displayed, try restarting the iPhone and it will work in my personal test.
Insert image description here

2. Add
a description to the info.plist file to customize it according to your own app.

<key>NSUserTrackingUsageDescription</key>
	<string>是否允许xx使用您的IDFA信息,以此来优化您的使用体验</string>

3. Obtain ATT permission during app initialization

+ (void)getAdvertisingTrackingAuthority {
    
    
    if (@available(iOS 14, *)) {
    
    
            ATTrackingManagerAuthorizationStatus status = ATTrackingManager.trackingAuthorizationStatus;
            switch (status) {
    
    
                case ATTrackingManagerAuthorizationStatusDenied:
                    NSLog(@"用户拒绝IDFA");
                    break;
                case ATTrackingManagerAuthorizationStatusAuthorized:
                    NSLog(@"用户允许IDFA");
                    break;
                case ATTrackingManagerAuthorizationStatusNotDetermined: {
    
    
                    NSLog(@"用户未做选择或未弹窗IDFA"); 
                    //请求弹出用户授权框,只会在程序运行是弹框1次,除非卸载app重装,通地图、相机等权限弹框一样
                    [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
    
    
                        NSLog(@"app追踪IDFA权限:%lu",(unsigned long)status);
                    }];
                }
                    break;
                default:
                    break;
            }
        } else {
    
    
            // Fallback on earlier versions
           if ([ASIdentifierManager.sharedManager isAdvertisingTrackingEnabled]) {
    
    
               NSLog(@"用户开启了广告追踪IDFA");
           }else {
    
    
              NSLog(@"用户关闭了广告追踪IDFA");
           }
        }
}

4. Problem description
Phenomenon: When we request to call up the IDFA authorization pop-up box, the program will still go down and will not wait for the user to select before continuing. At this time, if you call up the pop-up box and there is Get IDFA, you can get it, because the app runs by default before the user selects it, so how to synchronize it? You can perform subsequent logic after authorization is completed, as follows:

After the authorization operation is completed, perform the relevant acquisition operation eg the pseudo code below

case ATTrackingManagerAuthorizationStatusDenied: {
    
    
                    NSLog(@"用户拒绝IDFA");
                    finishBlock();
                    }
                    break;
case ATTrackingManagerAuthorizationStatusAuthorized: {
    
    
                    NSLog(@"用户允许IDFA");
                    finishBlock();
                    }
                    break;
case ATTrackingManagerAuthorizationStatusNotDetermined: {
    
    
                    NSLog(@"用户未做选择或未弹窗IDFA");
                    [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
    
    
                        dispatch_async(dispatch_get_main_queue(), ^{
    
    
                        finishBlock();
                    });
                }
                    break;

注意:Remember not to add a lock here to achieve synchronization. Depending on the calling scenario, deadlock may occur (eg: didFinishLaunchingWithOptions)

Guess you like

Origin blog.csdn.net/haifangnihao/article/details/113617158