How to get IDFA in iOS 14
What is IDFA?
It is a unique id (UUID) specific to each iOS device that is used to serve targeted ads and track app attribution.
It was introduced in iOS6 and up until iOS13 accessing it was a piece of cake.
import AdSupportvar idfa: UUID {
return ASIdentifierManager.shared().advertisingIdentifier
}
iOS14
Apps targeted towards iOS14 will need to use AppTrackingTransparency
framework along with AdSupport
framework to get the IDFA.
AppTrackingTransparency
allows developer to request permission from the user to read IDFA instead of just getting the IDFA from the ASIdentifierManager.
Note: You could only request the permission once per app install
Update Info.plist
Add message that informs the user why an app is requesting permission to use data for tracking the user or the device. This message goes in Info.plist
file of the iOS app.
//Privacy - Tracking Usage Description
<key>NSUserTrackingUsageDescription</key>
<string>App would like to access IDFA for tracking purpose</string>
Once you have added the message to the Info.plist
update your local code to account for requesting permission.
Request Permission
- Status: Authorized
Get IDFA using ASIdentifierManager - Status: Denied
Send user to settings page - Status: Not Determined
You can ask user for permission and show dialog
import AdSupport
import AppTrackingTransparencyfunc requestPermission() {
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")
}
}
}
This is how the request permission dialog looks like from Apple. If user denies it then we get all 0s in the UUID. The text in blue is what the developer can customize in their Info plist file of the app.
Links:
- https://developer.apple.com/documentation/bundleresources/information_property_list/nsusertrackingusagedescription
- https://developer.apple.com/documentation/adsupport/asidentifiermanager
- https://developer.apple.com/videos/play/wwdc2020/10676/
- Sample Project: https://github.com/nbhasin2/SampleIDFA/tree/main