What’s New With Privacy?

Learn about the new privacy features introduced in iOS 14.5, including accessing the new privacy-focused image picker, handling location permissions and more. By Renan Benatti Dias.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

iOS 14.5’s New App Tracking Transparency

Until now, apps have had access to a unique device identifier to track user activity between apps and websites and to target specific ads, without any explicit permission from the user. Starting with iOS 14.5, Apple requires every app that tracks users to explicitly request authorization to access the device identifier, giving users control over their privacy.

Each app that tracks users must request this permission, leaving it completely up to users the choice of which apps they do or don’t want to share their activity with.

Note: Users can open the Settings app and see a list of apps that track user activity. They can change their permission and even deny tracking to all apps, even for new apps that didn’t ask for permission yet.

Requesting Permission to Track the User Activity

To access the unique device identifier, you first have to request tracking authorization from the user. To do that, you’ll use the new AppTrackingTransparency framework to ask for user authorization.

Open Info.plist and add the following key:

  • Privacy – Tracking Usage Description with a value of YourPrivacy tracks your data to provide personalized ads to you.

This key gives iOS the reason why the app wants to track the user. iOS uses this text when asking the user permission to track them.

Open AppMain.swift and add the following at the top of the file:

import AppTrackingTransparency

Now, add the following extension at the end of the file:

// MARK: - App Tracking Transparency and Advertising Identifier
extension AppMain {
  func showAppTrackingAlert() {
    ATTrackingManager.requestTrackingAuthorization { _ in
      // TODO: Print Advertising Identifier
    }
  }
}

Here, you call requestTrackingAuthorization(completionHandler:) to prompt an alert asking permission to track the user. Once the user accepts or denies, ATTrackingManager calls the completion handler with an AuthorizationStatus.

The app’s authorizations status can be one of the following:

  1. .notDetermined: The user has not yet received a request to allow access to app tracking.
  2. .denied: The user has denied app tracking for the app or has denied app tracking for every app in the Settings app.
  3. .restricted: There’s a restriction to track the app. If the status is .restricted, the system won’t present the alert requesting authorization.
  4. .authorized: The user has authorized tracking for your app.

Once the user has allowed tracking, the system returns a unique identifier for the device that you use to track the user activity.

  1. In the simulator.
  2. On macOS.
  3. On iOS 14.5 if you haven’t requested app tracking permission yet, or if the user has denied app tracking.
  4. When a profile or configuration restricts app tracking.
Note: This advertising identifier returns as all zeroes in some specific conditions:
  1. In the simulator.
  2. On macOS.
  3. On iOS 14.5 if you haven’t requested app tracking permission yet, or if the user has denied app tracking.
  4. When a profile or configuration restricts app tracking.

Retrieving the Advertising Identifier

Using the AdSupport framework, you can access the unique device identifier to track the user.

Still in AppMain.swift, add the following at the top of the file:

import AdSupport

Next, add the following code at the end of the class extension:

func printAdvertisingIdentifier() {
  // 1
  guard ATTrackingManager.trackingAuthorizationStatus == .authorized else { 
    return 
  }
  // 2
  let advertisingIdentifier = ASIdentifierManager.shared()
    .advertisingIdentifier
  // 3
  print("Advertising Identifier: \(advertisingIdentifier)")
}

Here’s what’s happening:

  1. Make sure the user has allowed app tracking.
  2. Use ASIdentifierManager shared instance to retrieve the advertising identifier.
  3. Use this advertising identifier to send to a third party or a tracking service. Here, you simply print the identifier to the console.

Next, find // TODO: Print Advertising Identifier and replace with the following:

printAdvertisingIdentifier()

This calls the method for retrieving and printing the advertising identifier on the console.

Finally, add the following modifiers after // TODO: Add onAppear(perform:) for App Tracking:

.onAppear(perform: printAdvertisingIdentifier)
.onAppear(perform: showAppTrackingAlert)

This code adds two modifiers. The first tries to print the advertising identifier. If the user has not yet authorized app tracking, nothing happens. The second tries to request authorization for tracking from the user.

Build and run to see the app tracking alert.

Alert asking app to track the user activity for targeted ads

After allowing tracking, take a look at the console to see the advertising identifier.

Xcode console with the device advertising identifier

Where to Go From Here?

Download the final project by clicking the Download Materials button at the top or bottom of the tutorial.

In this tutorial, you learned how Apple is improving user privacy by adding new permissions and features to make apps more transparent and give control back to users. Apple strives to keep users’ privacy safe and secure.

If you want to dive more deeply into new restrictions and permissions for Local Networks, look at Apple’s session Support local network privacy in your app from WWDC 2020.

We hope you enjoyed this tutorial. If you have any questions or comments, please join the forum discussion below!