Swift: How to display permission to track in iOS 14.5 and above in Swift?
Displaying permission to track is a professional way to inform the user of the application that this application will use the app-related data to track the user or device. The user can either choose “Allow” option which is an opt-in for tracking or “Ask App Not to Track” option which is an opt-out from tracking.
You can think about this feature as similar to a permission which shows two options, “Allow cookies” or “Decline cookies” when we visit a website.
To whom is this article for?
If you are going to publish an application in iOS and are targeting users who are on a device version of iOS 14.5 and above. You need to follow the guidelines mentioned in Apple developer documentation — Asking Permission to Track
How to display permission?
To display permission, we need to implement the methods of the framework — App Tracking Transparency
You must import the framework in your project
import AppTrackingTransparency
As this framework, methods are available only for iOS 14 and above.
- You must add an if statement to check if the iOS version is 14 and above.
- Use the method
requestTrackingAuthorization(completionHandler:)
to display permission to track.
Note: This is permission will only be displayed once to the user during the first time the user has opened the application.
if #available(iOS 14, *) {
// Display permission to track
ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
switch status {
case .notDetermined:
print("Unknown consent")
case .restricted:
print("Device has an MDM solution applied")
case .denied:
print("Denied consent")
case .authorized:
print("Granted consent")
default:
print("Unknown")
}
})
}
The above code will display permission to track the user.
- The case
.authorized
meant the user has authorized the application. - The case
.restricted
meant the display permission not shown to the user and also tracking restricted. - The case
.denied
meant the user has not authorized the application. - The case
.notDetermined
meant the application cannot determine the authorisation status for access.
I hope my article helps you understand how to display permission in iOS using Swift.
If you like my articles, please follow me. You can also support me by buying me a coffee.