using System; #if UNITY_IOS using UnityEditor.iOS.Xcode; #endif using System.IO; using UnityEditor; using UnityEditor.Callbacks; using UnityEngine; using static RocketGMAppsFlyer.RocGMAppsFlyerController; namespace RocketGMAdjust.Editor { public class RocAppsFlyerGMBuildInjector { private const string _defaultTargetName = "Unity-iPhone"; [PostProcessBuild(1000)] public static void OnPostprocessBuild(BuildTarget buildTarget, string path) { #if UNITY_IOS if (buildTarget != BuildTarget.iOS) return; var projPath = PBXProject.GetPBXProjectPath(path); var xcodeProject = new PBXProject(); xcodeProject.ReadFromFile(projPath); Debug.Log(string.Format("PBX Project path: {0}", (object) projPath)); #if UNITY_2019_3_OR_NEWER var targetGuid = xcodeProject.GetUnityMainTargetGuid(); #else var targetGuid = xcodeProject.TargetGuidByName(_defaultTargetName); #endif Debug.Log(string.Format("Target GUID: {0}", targetGuid)); try { Debug.Log("Trying to ad AdSupport.framework to the Xcode project."); xcodeProject.AddFrameworkToProject(targetGuid, "AdSupport.framework", true); // Forcing another write to file operation just to make sure it is not producing exceptions. xcodeProject.WriteToFile(projPath); } catch (Exception exception) { Debug.LogError($"Failed to add AdSupport.framework to the Xcode project, the reason was: {exception.Message}"); } xcodeProject.SetBuildProperty(targetGuid, "SWIFT_VERSION", "5.0"); xcodeProject.WriteToFile(projPath); var entitlementsFilePath = GetEntitlementsPath(path, xcodeProject, targetGuid, _defaultTargetName); #if UNITY_2019_3_OR_NEWER var capabilityManager = new ProjectCapabilityManager(projPath, entitlementsFilePath, _defaultTargetName, targetGuid); #else var capabilityManager = new ProjectCapabilityManager(projPath, entitlementsFilePath, _defaultTargetName); #endif // Get plist string plistPath = string.Format("{0}{1}Info.plist", path, Path.DirectorySeparatorChar); PlistDocument plist = new PlistDocument(); plist.ReadFromString(File.ReadAllText(plistPath)); // Get root PlistElementDict rootDict = plist.root; PlistElement bundleUrlTypeElement = rootDict["CFBundleURLTypes"]; var bundleUrlTypes = bundleUrlTypeElement == null ? rootDict.CreateArray("CFBundleURLTypes") : bundleUrlTypeElement.AsArray(); //URL SCHEMES var bundleUrlTypeDict = bundleUrlTypes.AddDict(); bundleUrlTypeDict["CFBundleURLName"] = new PlistElementString(PlayerSettings.applicationIdentifier); var schemeArray = bundleUrlTypeDict.CreateArray("CFBundleURLSchemes"); using (StreamReader r = new StreamReader(Application.dataPath + "/RocketGames/RocGMAppsFlyerSdkConfig.txt")) { string json = r.ReadToEnd(); AppsFlyerUniversalLinkingConfig networkConfig = JsonUtility.FromJson(json); schemeArray.AddString(networkConfig.SubDomain); capabilityManager.AddAssociatedDomains(new[] { "applinks:" + networkConfig.Domain }); } //SKAN postback copies rootDict.SetString("NSAdvertisingAttributionReportEndpoint", "https://appsflyer-skadnetwork.com/"); // Write to file File.WriteAllText(plistPath, plist.WriteToString()); capabilityManager.WriteToFile(); #endif } #if UNITY_IOS // Returns exisiting file if found, otherwises provides a default name to use private static string GetEntitlementsPath(string path, PBXProject project, string targetGUID, string targetName) { // Check if there is already an eltitlements file configured in the Xcode project #if UNITY_2018_2_OR_NEWER var relativeEntitlementPath = project.GetBuildPropertyForConfig(targetGUID, "CODE_SIGN_ENTITLEMENTS"); if (relativeEntitlementPath != null) { var entitlementPath = path + Path.DirectorySeparatorChar + relativeEntitlementPath; if (File.Exists(entitlementPath)) { return entitlementPath; } } #endif // No existing file, use a new name return path + Path.DirectorySeparatorChar + targetName + Path.DirectorySeparatorChar + targetName + ".entitlements"; } #endif } }