Mark
Mark

Reputation: 3738

How to add the AppTrackingTransparency permission to your iOS apps

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.

Upvotes: 70

Views: 116286

Answers (12)

I followed some of the answers here but the only working one was writing the ATTrackingManager.requestTrackingAuthorization in the ViewController.swift.

In Info.plist, I wrote:

<key>NSUserTrackingUsageDescription</key>
<string>We use this data to provide you with a better advertising experience.</string>

And in the ViewController.swift, I wrote:

import AppTrackingTransparency


override func viewDidAppear(_ animated: Bool) {
       super.viewDidAppear(animated)
       requestTrackingPermission()
}
        
func requestTrackingPermission() {
       if #available(iOS 14, *) {
           ATTrackingManager.requestTrackingAuthorization { status in
               switch status {
                   case .notDetermined:
                        print("Tracking authorization not determined")
                   case .restricted:
                        print("Tracking authorization restricted")
                   case .denied:
                        print("Tracking authorization denied")
                   case .authorized:
                        print("Tracking authorization granted")
                   @unknown default:
                        print("Unknown tracking authorization status")
                }
           }
     } 
    else {
                print("AppTrackingTransparency framework not available on this iOS version")
            }
        }

To test if the request appear, you should not test it on the simulator.

Upvotes: 0

Pranav Kasetti
Pranav Kasetti

Reputation: 9915

Your complete guide to ATT (updated for iOS 15) 🌈

tl;dr

func applicationDidBecomeActive(_ application: UIApplication) {
    if #available(iOS 14, *) {
        ATTrackingManager.requestTrackingAuthorization { status in
            switch status {
                case .authorized:
                    print("enable tracking")
                case .denied:
                    print("disable tracking")
                default:
                    print("disable tracking")
            }
        }
    }
}

Long answer

All new apps submitted to the App Store need to follow App Tracking Transparency guidelines in iOS 14.0+. These guidelines form part of new Apple privacy guidelines. The main idea is for users to be given control of whether all apps can track them, some apps can track them, and make the privacy policies of the apps Transparent on download. :+1: Apple :wink:

1. Add framework in Xcode

This is possible by navigating to <PROJECT_NAME>.xcproject / <PROJECT_NAME>.xcworkspace -> General -> Frameworks, Libraries, and Embedded Content.

General Xcode Tab with ATT.framework

2. Add NSUserTrackingUsageDescription

This is a String key that needs to be added to Info.plist/ the Xcode project's Information tab (.xcodeproj or .xcworkspace files).

3. ATTrackingManager.requestTrackingAuthorizationWithCompletionHandler:

This function is advised on the first app launch to ensure the value is captured. The prompt only shows if the app is a fresh install and the user consent status is unknown.

For the majority of applications, only enable tracking if the status is authorized on becoming active (new in iOS 15), as below:

import AppTrackingTransparency

class AppDelegate: UIApplicationDelegate {

    func applicationDidBecomeActive(_ application: UIApplication) {
        if #available(iOS 14, *) {
            ATTrackingManager.requestTrackingAuthorization { status in
                switch status {
                    case .authorized:
                        print("enable tracking")
                    case .denied:
                        print("disable tracking")
                    default:
                        print("disable tracking")
                }
            }
        }
    }
}

NOTE: UI Logic needs to be wrapped on the DispatchQueue.main queue because the completion block currently executes on a concurrent DispatchQueue.

4. ATTrackingManager.trackingAuthorizationStatus

Track changes to consent via ATTrackingManager.trackingAuthorizationStatus which has 4 possible enum values:

  1. ATTrackingManagerAuthorizationStatusAuthorized - Granted consent.
  2. ATTrackingManagerAuthorizationStatusDenied - Denied consent.
  3. ATTrackingManagerAuthorizationStatusNotDetermined - Unknown consent.
  4. ATTrackingManagerAuthorizationStatusRestricted - Device has an MDM solution applied. Recommend handling this the same as consent denied until vendor provides consent explicitly.

5. Track the user's consent status on internal servers

If you are capturing your own analytics, this step is necessary because the user can toggle consent at any time using iOS Settings.

3rd-Party Analytics

Recommendations

  1. Disable Firebase, Adobe, mParticle Analytics, or other Analytics providers from the configuration on app launch after reading the status value when consent is denied/restricted. This is the safest option.
  2. Enable the optOut setting of the framework with anonymised tracking enabled when consent is denied/restricted. e.g. mParticle has this option to make the SDK conform to ATT requirements.
  3. Check the SDK documentation to see whether they leverage user identifiable information for tracking (see nuances with Firebase policy below).

Choose one of the three options based on your situation. It's hard to provide advice relevant to all SDKs, so you should consult the documentation to make an informed choice.

Reasoning

  1. Even if the frameworks don't track users for the moment, the SDKs may update and alter their policies down the line, and your code will not be synced automatically.
  2. There are lots of nuances in the Apple privacy legislation, and it's better to avoid the risk of getting store blacklisted IMHO.
  3. Technically you can hide from the Apple privacy guidelines, but do you want to get exposed when they start verifying the accuracy of your App Store statements? This could lead to permanent blacklisting.

Example Firebase policy (26.06.20)

To answer your questions:

  • No, Analytics will not access the ad ID if no advertising SDKs are present and AdSupport is not linked. However, SafariServices on iOS 11 imports the AdSupport framework, causing device advertisement identifier reporting. #1686 (GH issue #) makes it so that explicit ad ID access control is required, which is something we need to add on the Firebase side.
  • Yes, if you're using Analytics with an ad framework you should follow Apple's guidelines. You may not be able to submit to the App Store otherwise.

Other Option

SO, yes it is possible to work around declaring whether you use analytics, but I don’t advise it.

App Store Analytics might be enough for smaller apps and has the advantage of being easier to comply with ATT requirements. These are viewable on App Store Connect. Third-party analytics may provide more detailed data and broader coverage, but Apple Analytics delivers a more seamless, integrated experience. However, I realise many companies heavily rely on 3rd party analytics data, and if you aren't able to migrate away, transparently declare your analytics usage.

Another ideal strategy would be to fully revamp or rapidly A/B test the app before launching live - use beta modes. Analytics in the App Store is more than enough for live apps, and makes the app more appealing to customers by clearly declaring tracking permissions at the time of download.

I can't quantify the risk for being blacklisted subsequently though, as this is library-specific and also depends on your release workflow (do you check for changes in SDK policies?).

Important Note

Wrap

if #available(iOS 14.0, *)

Wherever you call the ATTrackingManager because the request will not complete on older iOS versions 😅. Track consent on older iOS versions with your own backend flags, or locally on the device.

Apple suggests implementing the new ATT requirements as soon as possible if you track users because you will be blocked from new updates to the App Store in the meantime, even with production crashes. Not only will your users be happier, but your App Store ranking improves if you update your app regularly.

Want to toggle user consent in the app? See here for more info.

Upvotes: 40

S. Gissel
S. Gissel

Reputation: 2650

For me embedding the dialog into App Delegate applicationDidBecomeActive wasn't working.

I embedded the well known ATTrackingManager.requestTrackingAuthorization into my initial viewcontroller.

Upvotes: 0

stm apps
stm apps

Reputation: 61

func askPermission() {
    if #available(iOS 14, *) {
        ATTrackingManager.requestTrackingAuthorization { (status) in
            //handled
            print(status)
            if status == .authorized{
                
                Settings.shared.isAdvertiserTrackingEnabled = true
                
            }
        }
    }
}

I don't know but for me this piece of code works

Upvotes: 3

Timchang Wuyep
Timchang Wuyep

Reputation: 811

Calling ATTrackingManager.requestTrackingAuthorization in the App Delegate applicationDidBecomeActive function won't be called if you're using Scenes because of this instead use sceneDidBecomeActive(_:) in the SceneDelegate and make sure you delay for a second else it won't be called for some reason.

func sceneDidBecomeActive(_ scene: UIScene) {
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
        
        if #available(iOS 14, *) {
            ATTrackingManager.requestTrackingAuthorization { status in
                switch status {
                    case .authorized:
                        print("enable tracking")
                    case .denied:
                        print("disable tracking")
                    default:
                        print("default tracking")
                }
            }
        }
        
    })
   }

Upvotes: 5

Mahfuz
Mahfuz

Reputation: 93

I have observed that if the following method

"ATTrackingManager requestTrackingAuthorizationWithCompletionHandler()"

is called from AppDelegate then sometimes "ATTrackingManagerAuthorizationStatusNotDetermined" is returned as a status and an alert is not shown.
My phone's iOS version is "iOS-15.0.2"

As a solution, we can call "requestTrackingAuthorization..." method after splash screen or after appearing LandingPage.

Upvotes: 0

tsik
tsik

Reputation: 629

We are using Firebase and Facebook, to get the app approved we put both calls "behind" the ATT protection, that is:

For Facebook (from AppDelegate):

if #available(iOS 14, *) {
    ATTrackingManager.requestTrackingAuthorization { status in
        if status == .authorized {
                ApplicationDelegate.shared.application(
                    application,
                    didFinishLaunchingWithOptions: launchOptions
                )
        }
    }
}

For Firebase:

if #available(iOS 14, *) {
    ATTrackingManager.requestTrackingAuthorization { status in
        if status == .authorized {
            Analytics.logEvent(eventName, parameters: [:])
        }
    }
}

As for the firebase token for the notifications, we did not include them behind the protection and had no issue.

Upvotes: 5

bartwader
bartwader

Reputation: 404

In iOS 15 it can only be requested with ATTrackingManager.requestTrackingAuthorization if the application state is already active, so it should be moved from didFinishLaunchingWithOptions to applicationDidBecomeActive.

Upvotes: 36

CopsOnRoad
CopsOnRoad

Reputation: 267384

  1. Open info.plist file, add SKAdNetworkIdentifier and NSUserTrackingUsageDescription. The following one is only for Google (Admob), you can find the full list here.

    <key>SKAdNetworkItems</key>
    <array>
        <dict>
            <key>SKAdNetworkIdentifier</key>
            <string>cstr6suwn9.skadnetwork</string>
        </dict>
    </array>
    
    //..
    
    <key>NSUserTrackingUsageDescription</key>
    <string>This identifier will be used to deliver personalized ads to you.</string>
    
  2. Request the ATT dialog. (For simplicity I'm requesting it right after the app loads)

    #import <AppTrackingTransparency/AppTrackingTransparency.h>
    #import <AdSupport/AdSupport.h>
    
    //...
    
    - (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        // ...
    
        if (@available(iOS 14.0, *))
        {
            [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            // Tracking authorization completed. Start loading ads here.
            // [self loadAd];
          }];
    
      return [super application:application didFinishLaunchingWithOptions:launchOptions];
    
    }
    

Upvotes: 3

Bbx
Bbx

Reputation: 3384

According to the link you posted:

https://developers.google.com/admob/ios/ios14#swift.

The main thing to avoid loss of revenue from AdMob is to add this into Info.plist:

<key>SKAdNetworkItems</key>
<array>
  <dict>
    <key>SKAdNetworkIdentifier</key>
    <string>cstr6suwn9.skadnetwork</string>
  </dict>
</array>

I don't know when/if AppTrackingTransparency permission is required. I know Apple say you need it but they don't give a deadline. Google say "If you decide to include App Tracking Transparency" which to me is hinting to not bother!

Upvotes: 4

Sherif Kamal
Sherif Kamal

Reputation: 31

As a completion on @mark answer you also should add permission statement to plist file in key Privacy - Tracking Usage Description

Upvotes: 1

Mark
Mark

Reputation: 3738

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

Upvotes: 74

Related Questions