| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using UnityEngine;
- #if UNITY_IOS && !UNITY_EDITOR
- using Unity.Advertisement.IosSupport;
- #endif
- /// <summary>Runs the iOS ATT dialog once before SDKs that may read IDFA are initialized.</summary>
- public static class IosAppTrackingService
- {
- static bool completed;
- static bool inFlight;
- static SynchronizationContext mainThreadContext;
- static int mainThreadId;
- static readonly List<Action> pendingCallbacks = new List<Action>();
- public static bool IsCompleted => completed;
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
- static void ResetStatics()
- {
- completed = false;
- inFlight = false;
- mainThreadContext = null;
- mainThreadId = 0;
- pendingCallbacks.Clear();
- }
- public static void RunOnce(Action onComplete)
- {
- if (onComplete == null)
- return;
- if (completed)
- {
- onComplete();
- return;
- }
- if (mainThreadContext == null)
- {
- mainThreadContext = SynchronizationContext.Current;
- mainThreadId = Thread.CurrentThread.ManagedThreadId;
- }
- pendingCallbacks.Add(onComplete);
- if (inFlight)
- return;
- #if UNITY_IOS && !UNITY_EDITOR
- if (Application.platform != RuntimePlatform.IPhonePlayer)
- {
- Complete();
- return;
- }
- var status = ATTrackingStatusBinding.GetAuthorizationTrackingStatus();
- if (status != ATTrackingStatusBinding.AuthorizationTrackingStatus.NOT_DETERMINED)
- {
- Debug.Log("[ATT] Tracking authorization status is already determined: " + status);
- Complete();
- return;
- }
- inFlight = true;
- Debug.Log("[ATT] Requesting App Tracking Transparency authorization.");
- ATTrackingStatusBinding.RequestAuthorizationTracking(CompleteRequest);
- #else
- Complete();
- #endif
- }
- #if UNITY_IOS && !UNITY_EDITOR
- static void CompleteRequest(int status)
- {
- Debug.Log("[ATT] Authorization request completed with status: " + status);
- SynchronizationContext context = mainThreadContext;
- if (Thread.CurrentThread.ManagedThreadId != mainThreadId && context != null)
- {
- context.Post(_ => Complete(), null);
- return;
- }
- Complete();
- }
- #endif
- static void Complete()
- {
- completed = true;
- inFlight = false;
- var callbacks = pendingCallbacks.ToArray();
- pendingCallbacks.Clear();
- foreach (Action callback in callbacks)
- callback?.Invoke();
- }
- }
|