With Apple’s incoming changes to therequirements for use of IDFAand needing to request users permission via App Tracking Transparency popup which is incoming at some point in early 2021. Apps need to start to prepare to the incoming changes.
At the moment there is no build in Unity method that will trigger the notification, There is however a preview package!
As a note App Tracking Transparency is only required on IOS 14 devices, Altough this won’t causing issues on any lower OS version it won’t trigger the popup. If you don’t have access to a IOS 14 device you can test this with the XCode Emulators.
1. Download Unity IOS Support Package
Download the com.unity.ads.ios.support library fromGithub. This is current in preview and won’t be found directly from the package manager.
Unzip this into a folder outside your Asset Folder.
Note it is best to make sure these files are checking into source control we will be using a relative path from Unity package manager.
2. Add to Package Manager
Now in you’re unity project go Windows->Package Manger.
Then press the plus button and add from disk and navigate to the package.json file within the com.unity.ads.ios.support that we just unzipped.
Unity will then import the package, Because we have imported the file from within the base Unity Project file the file has been direct to a relative path, if you however place this file outside this folder you will need to edit the Packages/manifest.json file to make it relative.
3. Triggering App Tracking Transparency Request
As most SDK recommend We will need to trigger this before we initialize any SDK’s that we wish to pass the IDFA into. We also can only trigger this one pre install with users being able to edit there preference within the IOS Settings menu.Here we are using a really simple system where we trigger the notification when a user press a button.
Breaking down
We first state we are using the IosSupport Library. We also inclosing this with aUNITY_IOSpre-compile which means we will only compile this code if we are running on IOS
Next the actual triggering, again inclosing this with theUNITY_IOSpre-compile. We first check callATTrackingStatusBinding.GetAuthorizationTrackingStatus()which will tell if the user has already set there preference, thenATTrackingStatusBinding.RequestAuthorizationTracking()Which will trigger the popup.
Like any other permission request Apple requires a description on why you are requesting these permission. For this we need to add theNSUserTrackingUsageDescriptionkey to our PList. Instead of having to do this each time we create a build we are going to create a post build step that automatically adds it to the XCode Project
You can see more about Unity post build stepshere
usingUnityEditor;
usingUnityEditor.Callbacks;
#ifUNITY_IOS
usingUnityEditor.iOS.Xcode;
#endif
usingSystem.IO;
publicclassPostBuildStep
{
/// <summary>
/// Description for IDFA request notification
/// [sets NSUserTrackingUsageDescription]
/// </summary>
conststringTrackingDescription=
"This identifier will be used to deliver personalized ads to you. ";
Here we just read in the PlistFile from the Xcode project we just created, then add in a new string valuesplistRoot.SetString(“NSUserTrackingUsageDescription”, TrackingDescription);Which sets the our popup text.
4. Testing it out
Build the App to Xcode as normal and open it up in Xcode. If you open up theInfo.Plistyou will see the description we just added in from the post build step
Now we can just build and play on our device. Then just press the button and you should see the IDFA Popup!
You will notice that if you press the button again it won’t re-trigger this is by the apple requirements so we will need to uninstall and pre-install to test it again.
This is the basic implementation ahead of the requirement of App Tracking Transparency for IOS, for a production version it may be best to pre-warn users about the popup, explaining why you are wanting to use the IDFA. This will likely improve the number of user who opt-in