Evelyn GameDev Logo
Published on

(Unity) Support for ATT (App Tracking Transparency) in iOS14.5 or later

Authors
  • avatar
    Evelyn
(Unity) Support for ATT (App Tracking Transparency) in iOS14.5 or later

Please share this article if you like!

If you have visited this article, you are probably developing a smartphone game made by Unity.

icon of Evelyn
I want to support ATT (App Tracking Transparency) for IOS, but how do I do that?

For those of you with that problem, here's how to deal with ATT! By the way, I'm using Unity Ads as a means of monetization.

The content of this article is also available in video format, so if you have any questions as you read through the article, please check the video as well.


If you are planning to use Unity Ads, please watch this video.


Table of Contents

Create an Objective C file under Plugins > iOS

First, create the native iOS code in the following location.

Plugins > iOS > RequestIDFA.mm

#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import <AdSupport/AdSupport.h>

extern "C" void _requestIDFA() {
    if (@available(iOS 14.5,*)) {
        [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
            // Tracking authorization completed. Start loading ads here.
            // [self loadAd];
          }];
    }
}
Plug-ins for iOS

Display a dialog to ask the user for permission to use IDFA

Next, create a C# script to call the native file you have just installed in the game. Then attach it to a game object in any scene.

IDFA.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;

public class IDFA : MonoBehaviour
{
#if UNITY_IOS
    [DllImport("__Internal")]
    private static extern void _requestIDFA();
#endif


    private void Start()
    {
#if UNITY_IOS
        _requestIDFA();
#endif
    }
}
request IDFA from game scene

Create a PostBuild script for Unity to automate the configuration in Xcode

It's a hassle to configure every single setting in Xcode, so the last step is to create a build script to automate the configuration for iOS.

Let's place it under the Editor folder.

Editor > AddElementsInfoplist.cs

using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using UnityEngine;

public class AddElementsInfoplist
{
    /// <summary>
    /// Post-build processing
    /// </summary>
    /// <param name="buildTarget">Build target information</param>
    /// <param name="path">output path</param>
    [PostProcessBuild(1)]
    public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
    {
        if (buildTarget == BuildTarget.iOS)
        {
            BuildIOS(buildTarget, path);
        }
    }

    /// <summary>
    /// Post-build processing for iOS
    /// </summary>
    /// <param name="buildTarget">Build target information</param>
    /// <param name="path">output path</param>
    public static void BuildIOS(BuildTarget buildTarget, string path)
    {
        var exportPath = new DirectoryInfo(path).FullName;
        var projectPath = new DirectoryInfo(
            Path.Combine(Path.Combine(exportPath, "Unity-iPhone.xcodeproj"), "project.pbxproj")).FullName;

        // Load the Xcode project file output by Unity
        var project = new PBXProject();
        project.ReadFromFile(projectPath);

        // Target reference in Xcode project
        var frameworks = project.GetUnityMainTargetGuid();

        // Add framework reference for ATT
        project.AddFrameworkToProject(frameworks, "AppTrackingTransparency.framework", false);

        // Rewrite Info.plist
        var plist = new PlistDocument();
        var plistPath = Path.Combine(path, "Info.plist");
        plist.ReadFromFile(plistPath);

        // Add description for ATT
        plist.root.SetString("NSUserTrackingUsageDescription", "The above information will be used to display ads and analyze usage.");

        // Write back to Xcode project file
        plist.WriteToFile(plistPath);
        project.WriteToFile(projectPath);
    }
}
post build script

Run on the device

Perform the above steps and try to run it on the device simulator. You will see a dialog about ATT as shown in the image below.

demo

Dealing with the error "Undefined symbols for architecture arm64: 'OBJC_CLASS$_ATTrackingManager', reference from: objc-class_ref in RequestIDFA.o"

When building with Xcode, you may have gotten the error "Undefined symbols for architecture arm64: 'OBJC_CLASS$_ATTrackingManager', reference from: objc-class_ref in You may get an error "RequestIDFA.o".

In that case, try adding the App Tracking Transparency.framework to the Linking Binary.

Fixing errors

If the error still persists, try placing the AppTrackingTransparency.framework under Plugins > iOS in your Unity project and rebuild it.

Right-click on AppTrackingTransparency.framework in Xcode to make it appear in the Finder.

Show in Finder

Next, drag and drop the AppTrackingTransparency.framework under the Plugins > iOS folder of your Unity project. Then build it for iOS and the error should be resolved.

Drag and Drop att framework to iOS folder
icon of Evelyn
ATT-enabled for iOS14 or later, boost your ad revenue per unit!

Thank you for reading to the end. I would be happy if I could contribute to your game development.

Please share this article if you like!