Unity iOS IDFA and GPS Setup


This guide will allow Unity SDK users to have access to IDFA (advertiser ID) and GPS coordinate data in their logged events.


iOS App Store Compliance

Note if an app is subject to COPPA because it is aimed towards kids, the app cannot contain any IDFA or GPS tracking code whatsoever. This is why the IDFA and GPS code is packaged separately and requires this setup.

First, take the two files unity-plugin/IdfaIOS/CustomIdfa.m and unity-plugin/IdfaIOS/CustomGPS.m and place them into Assets/Scripts. You may place the file wherever, but check all the #import statements lead to correct paths.

In the imports section (the top) of your .cs game script, add this import:

using System.Runtime.InteropServices;

Inside the game class, add the following code inside your MonoBehaviour class, or any other class.

public class AmplitudeDemo : MonoBehaviour {

    [DllImport ("__Internal")]
    private static extern void setIdfaBlock(string instanceName);
    [DllImport ("__Internal")]
    private static extern void setLocationInfoBlock(string instanceName);

Finally, in your game code, probably in void Start(), call these functions. YOUR_INSTANCE_NAME is a string associated with this particular instance of Amplitude. YOUR_INSTANCE_NAME may also be null or an empty string.

Amplitude amplitude = Amplitude.getInstance("YOUR_INSTANCE_NAME");
amplitude.init("e7177d872ff62c0356c973848c7bffba"); //API key


These functions will prompt the iOS user to accept or reject permissions for GPS location tracking, as well as IDFA tracking.

Furthermore, your Unity app needs two special configurations.
For location, please navigate to Unity > Edit > Project Settings.... The menu in the first image below will pop up. Select Player, then click the iOS tab. Click Other Settings, and scroll until the field Location Usage Description. Type a sentence that prompts the user for GPS tracking permissions into the textbox.


xcode Simulator

IDFA tracking permissions can generally only be tested reliably on real life phones.

For IDFA, the file Info.plist has to be edited according to Apple's specifications. This can be done with a Unity script with guidance from this Unity post.

Also, when the app is compiled into iOS and launches into xcode, find the top-level file Info.plist. Click the plus symbol next to any key value pair. Use the xcode editor to find the key Privacy - Tracking Usage Description, ensure the Type is String, and type a prompt to ask for tracking permission in the Value field.


What are alternatives to IDFA? Should I consider alternatives?
Yes, you should consider alternatives. Do not assume users will enable IDFA tracking; opt-in systems engage less. Use device id, IDFV, or pass your own app's email login system as a custom user property.

Can I edit the Objective-C iOS logic to fetch IDFA and GPS data?
Yes, you may. However, the current code was written to handle permissions, and accurately update the IDFA and GPS data within the SDK when the app user gives permissions.

Why was the code engineered this way? Why wasn't it just included inside the Unity SDK?
The Apple App Store will flag any app that uses IDFA code, even if the code is disabled or sourced from a third-party SDK developer like Amplitude.

Did this page help you?