[펌] App Tracking Transparency in Unity (IDFA Popup)
With Apple’s incoming changes to the requirements for use of IDFA and 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.
So how do you add a IDFA App Tracking Transparency popup from a Unity app?
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 from Github. 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 a UNITY_IOS pre-compile which means we will only compile this code if we are running on IOS
#if UNITY_IOS using Unity.Advertisement.IosSupport; #endif
Next the actual triggering, again inclosing this with the UNITY_IOS pre-compile. We first check call ATTrackingStatusBinding.GetAuthorizationTrackingStatus() which will tell if the user has already set there preference, then ATTrackingStatusBinding.RequestAuthorizationTracking() Which will trigger the popup.
#if UNITY_IOS if(ATTrackingStatusBinding.GetAuthorizationTrackingStatus() == ATTrackingStatusBinding.AuthorizationTrackingStatus.NOT_DETERMINED) { ATTrackingStatusBinding.RequestAuthorizationTracking(); } #endif
4. Post build steps
Like any other permission request Apple requires a description on why you are requesting these permission. For this we need to add the NSUserTrackingUsageDescription key 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 steps here
using UnityEditor; |
using UnityEditor.Callbacks; |
#if UNITY_IOS |
using UnityEditor.iOS.Xcode; |
#endif |
using System.IO; |
public class PostBuildStep |
{ |
/// <summary> |
/// Description for IDFA request notification |
/// [sets NSUserTrackingUsageDescription] |
/// </summary> |
const string TrackingDescription = |
"This identifier will be used to deliver personalized ads to you. "; |
[PostProcessBuild(0)] |
public static void OnPostprocessBuild(BuildTarget buildTarget, string pathToXcode) |
{ |
if (buildTarget == BuildTarget.iOS) |
{ |
AddPListValues(pathToXcode); |
} |
} |
static void AddPListValues(string pathToXcode) |
{ |
// Get Plist from Xcode project |
string plistPath = pathToXcode + "/Info.plist"; |
// Read in Plist |
PlistDocument plistObj = new PlistDocument(); |
plistObj.ReadFromString(File.ReadAllText(plistPath)); |
// set values from the root obj |
PlistElementDict plistRoot = plistObj.root; |
// Set value in plist |
plistRoot.SetString("NSUserTrackingUsageDescription", TrackingDescription); |
// save |
File.WriteAllText(plistPath, plistObj.WriteToString()); |
} |
} |
view rawPostBuild.cs hosted with ❤ by GitHub
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 the Info.Plist you 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
If any problems feel free to drop me a line on Twitter @Gamereat.
[출처] alanyeats.com/post/unityapptrackingtransparencypopup/
'Unity3D > iOS' 카테고리의 다른 글
[iOS][빌드에러] Code Signing "fccore.bundle" failed. (0) | 2021.09.03 |
---|---|
[Unity][iOS][RuntimeError] FBAdSettings is a final class and cannot be subclassed. (0) | 2021.04.30 |
[링크] iOS Supported Language Codes (ISO-639) (0) | 2021.02.25 |
[펌] Unity에서 Apple로 로그인 구현 (0) | 2021.01.27 |
[펌] IOS SWIFT bitcode 오류 해결 (does not contain bitcode) (0) | 2021.01.27 |