How to add the AppTrackingTransparency permission to your iOS apps

Mark picture Mark · Aug 25, 2020 · Viewed 8.2k times · Source

I am extremely new to iOS, with no iOS development experience at all, however, I've been given a task that's related to preparing for iOS 14+. Based on what I've found https://support.google.com/admanager/answer/9997589, to ensure there's no loss in revenue, I need to do 2 things.

  1. Install the latest Google Mobile Ads SDK for iOS (version 7.64 or later) for AdMob or Ad Manager
  2. Add the AppTrackingTransparency permission to your iOS apps.

I've followed some guides and I'm at the point of dealing with adding the AppTrackingTransparency permission to the iOS app. This is the guide that I'm using, https://developers.google.com/admob/ios/ios14#swift.

I managed to add the key/value, shown below, in Info.plist

<key>NSUserTrackingUsageDescription</key>
<string>This identifier will be used to deliver personalized ads to you.</string>

But this is where I'm hoping to get some help. I think that I still need to add code somewhere to request user permission with AppTrackingTransparency. Based on the guide I think the following code is required to show the App Tracking Transparency dialog box. Question 1, is my assumption correct?

import AppTrackingTransparency
import AdSupport
...
func requestIDFA() {
  ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
    // Tracking authorization completed. Start loading ads here.
    // loadAd()
  })
}

Question 2, does the code live in AppDelegate.swift? Or is it really just somewhere that's suitable in the codebase? Thanks.

Answer

Mark picture Mark · Aug 28, 2020

For those who might be struggling with the same things, I got the AppTrackingTransparency dialog box to appear with the function,

import AppTrackingTransparency
import AdSupport

//NEWLY ADDED PERMISSIONS FOR iOS 14
func requestPermission() {
    if #available(iOS 14, *) {
        ATTrackingManager.requestTrackingAuthorization { status in
            switch status {
            case .authorized:
                // Tracking authorization dialog was shown
                // and we are authorized
                print("Authorized")
                
                // Now that we are authorized we can get the IDFA
                print(ASIdentifierManager.shared().advertisingIdentifier)
            case .denied:
                // Tracking authorization dialog was
                // shown and permission is denied
                print("Denied")
            case .notDetermined:
                // Tracking authorization dialog has not been shown
                print("Not Determined")
            case .restricted:
                print("Restricted")
            @unknown default:
                print("Unknown")
            }
        }
    }
}
//

I then simply called the function requestPermission() on the app's login page, so users see the permission dialog before signing in. Without calling the function, the dialog box show in this guide, https://developers.google.com/admob/ios/ios14, doesn't appear for me.

This article has an example github project: https://medium.com/@nish.bhasin/how-to-get-idfa-in-ios14-54f7ea02aa42