[Unity] How to easily obtain IDFA tracking permission only within Unity.

Unity

Hello, this is HAL-JP.
By the way, everyone, when you get IDFA in Unity, do you create Objective-C or add framework on Xcode side? Objective-C or add framework on Xcode side? It’s a hassle to configure each and every time you build, isn’t it? With the method introduced here, if you set it up first, you don’t have to set it up every time you build. It is also convenient because it can display the IDFA description and other information.

What is IDFA Acquisition?

IDFA (Identifier for Advertisers) is a device ID randomly assigned by Apple to a user’s device. Advertisers can use this ID to measure user behavior within their apps to serve customized ads. However, with the release of IOS 14.5 everything has changed. You can no longer freely access a device’s IDFA, and with the release of IOS 14.5, apps must now display a pop-up dialog requesting permission to use the IDFA.

Obtain IDFA tracking permission

Now, it is time to obtain IDFA tracking permission!

  • STEP.1
    Install iOS 14 Advertising Support

    From Unity, select Window>Package Managerand from the Unity Registry, find iOS 14 Advertising Support and select “Install

  • STEP.2
    Show description of IDFA

    Select Import Samples

    Samples are created in the Asset folder upon import.

    Asset>Samples>iOS 14 Advertising Support>1.0.0>01 Context Screen>Prefabs>Context Screen.prefab to the scene you want to display IDFA description Drag and drop

  • STEP.3
    Set when to display

    Create a script named Example.cs and enter the following

    using System.Collections;
    using Unity.Advertisement.IosSupport;
    using UnityEngine;
    #if UNITY_IOS
    using UnityEngine.iOS;
    #endif
    public sealed class Example : MonoBehaviour
    {
        public GameObject ATT;
    #if UNITY_IOS
        private void Start()
        {
            if (ATTrackingStatusBinding.GetAuthorizationTrackingStatus() == ATTrackingStatusBinding.AuthorizationTrackingStatus.NOT_DETERMINED)
            {
                ATT.SetActive(true);
            }
        }
    #endif
    }
    
    

    Next, create an EventSystem

    Drag and drop Example.cs into the created EventSystem, and drag and drop Context Screen onto the ATT of Example.cs

    Finally, deactivate Context Screen

  • STEP.4
    Added description of pop-up dialog

    Create a folder named Editor in Asset
    In that folder, create a script named PostBuildStep.cs and enter the following
    *Change only the description of the popup dialog

    using UnityEditor;
    using UnityEditor.Callbacks;
    #if UNITY_IOS
    using UnityEditor.iOS.Xcode;
    #endif
    using System.IO;
     
    public class PostBuildStep {
        // Set the IDFA request description:
        const string k_TrackingDescription = "Description of pop-up dialog.";
     
        [PostProcessBuild(0)]
        public static void OnPostProcessBuild(BuildTarget buildTarget, string pathToXcode) {
            if (buildTarget == BuildTarget.iOS) {
                AddPListValues(pathToXcode);
            }
        }
     
        // Implement a function to read and write values to the plist file:
        static void AddPListValues(string pathToXcode) {
            // Retrieve the plist file from the Xcode project directory:
            string plistPath = pathToXcode + "/Info.plist";
            PlistDocument plistObj = new PlistDocument();
     
     
            // Read the values from the plist file:
            plistObj.ReadFromString(File.ReadAllText(plistPath));
     
            // Set values from the root object:
            PlistElementDict plistRoot = plistObj.root;
     
            // Set the description key-value in the plist:
            plistRoot.SetString("NSUserTrackingUsageDescription", k_TrackingDescription);
     
            // Save changes to the plist:
            File.WriteAllText(plistPath, plistObj.WriteToString());
        }
    }
  • STEP.5
    Close the popup dialog

    Open Assets/Samples/iOS 14 Advertising Support/1.0.0/01 Context Screen/Scripts/ContextScreenView.cs and add the following marker section

    using System;
    using UnityEngine;
    
    namespace Unity.Advertisement.IosSupport.Components
    {
        /// <summary>
        /// This component controls an iOS App Tracking Transparency context screen.
        /// You should only have one of these in your app.
        /// </summary>
        public sealed class ContextScreenView : MonoBehaviour
        {
            /// <summary>
            /// This event will be invoked after the ContinueButton is clicked
            /// and after the tracking authorization request has been sent.
            /// It's a good idea to subscribe to this event so you can destroy
            /// this GameObject to free up memory after it's no longer needed.
            /// Once the tracking authorization request has been sent, there's no
            /// need for this popup again until the app is uninstalled and reinstalled.
            /// </summary>
            public event Action sentTrackingAuthorizationRequest;
    
            public void RequestAuthorizationTracking()
            {
    #if UNITY_IOS
                Debug.Log("Unity iOS Support: Requesting iOS App Tracking Transparency native dialog.");
    
                ATTrackingStatusBinding.RequestAuthorizationTracking();
    
                sentTrackingAuthorizationRequest?.Invoke();
    #else
                Debug.LogWarning("Unity iOS Support: Tried to request iOS App Tracking Transparency native dialog, " +
                                 "but the current platform is not iOS.");
    #endif
                gameObject.SetActive(false);
            }
        }
    }
    

Finally, if you BUILD and run it on a terminal that has not rejected IDFA requests, the permission screen will appear.


This is how to easily get IDFA tracking permission just from within Unity. Get IDFA to increase your advertising revenue!

If you are not sure how to do this properly, please feel free to contact us.
Contact

Comment

Copied title and URL