How to automatically set associated domain for iOS app using Unity
Asked Answered
C

3

5

I am developing an iOS application using Unity right now and I am trying to set the associated domain without manually setting it on xcode. I believe I can achieve this with PostProcessBuild but I can't figure this

public class AvametricIOSBuildPostProcessor
{

    static string[] associatedDomains;
    [PostProcessBuild]
    public static void OnPostprocessBuild (BuildTarget target, string pathToBuiltProject)
    {
    if (target == BuildTarget.iOS)
        OnPostprocessBuildIOS (pathToBuiltProject);
    }

    private static void OnPostprocessBuildIOS (string pathToBuiltProject)
    {

       var projectCapabilityManager = new ProjectCapabilityManager(plistPath, plistPath, "Unity-iPhone");
       associatedDomains[0] = "applinks:XXXXX";
       projectCapabilityManager.AddAssociatedDomains(associatedDomains);


    }
}
Cavort answered 11/11, 2017 at 3:9 Comment(0)
S
7

I managed to add an associated domain by doing like so

private static void OnProstProcessBuildIOS(string pathToBuiltProject)
{
    //This is the default path to the default pbxproj file. Yours might be different
    string projectPath = "/Unity-iPhone.xcodeproj/project.pbxproj";
    //Default target name. Yours might be different
    string targetName = "Unity-iPhone";
    //Set the entitlements file name to what you want but make sure it has this extension
    string entitlementsFileName = "my_app.entitlements";

    var entitlements = new ProjectCapabilityManager(pathToBuiltProject + projectPath, entitlementsFileName, targetName);
    entitlements.AddAssociatedDomains(new string[] { "applinks:myurl.com" });
    //Apply
    entitlements.WriteToFile();
}

Don't forget to provide the path to the .pbxproj file not the .plist nor the project file itself.
Make sure you apply your changes with WriteToFile().

Associated Unity documentation about ProjectCapabilityManager and AddAssociatedDomains

Soso answered 19/3, 2018 at 17:23 Comment(1)
One more thing, configure it in Xcode: cs mProject.AddCapability(mTargetGUID, PBXCapabilityType.AssociatedDomains, entitlementsFileName); Zeralda
S
1

I solve the problem use the following code:

private static bool addPushAndAssociatedDomainCapability(PBXProject project, string pathToBuildProject)
    {
        var targetName = PBXProject.GetUnityTargetName();
        var targetGuid = project.TargetGuidByName(targetName);
        var productName = project.GetBuildPropertyForAnyConfig(targetGuid, "PRODUCT_NAME");
        var fileName = productName + ".entitlements";
        var entitleFilePath = pathToBuildProject + "/" + targetName + "/" + fileName;
        if (File.Exists(entitleFilePath) == true)
        {
            Debug.Log("[Builder] entitle file exist.");
            return true;
        }

        bool success = project.AddCapability(targetGuid, PBXCapabilityType.PushNotifications);
        if (success ==false)
        {
            Debug.Log("[Builder] open push cabability fail.");
            return false;
        }
        success = project.AddCapability(targetGuid, PBXCapabilityType.AssociatedDomains);

        const string entitlements = @"
     <?xml version=""1.0"" encoding=""UTF-8\""?>
     <!DOCTYPE plist PUBLIC ""-//Apple//DTD PLIST 1.0//EN"" ""http://www.apple.com/DTDs/PropertyList-1.0.dtd"">
     <plist version=""1.0"">
         <dict>
             <key>aps-environment</key>
             <string>development</string>
             <key>com.apple.developer.associated-domains</key>
             <array>
                <string>xxx</string>
             </array>
         </dict>
     </plist>";

        try
        {
            File.WriteAllText(entitleFilePath, entitlements);
            project.AddFile(targetName + "/" + fileName, fileName);
            project.AddBuildProperty(targetGuid, "CODE_SIGN_ENTITLEMENTS", targetName + "/" + fileName);
        }
        catch (IOException e)
        {
            Debug.LogError("Could not copy entitlements. Probably already exists." + e);
        }

        Debug.Log("[Builder] add push capability success.");
        return true;
    }
Sail answered 21/11, 2019 at 3:37 Comment(0)
S
0

In Unity 2021.3, Add blow .cs to Editor folder

using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.iOS.Xcode;
using UnityEngine;

public class UniversalLinkPostBuild : IPostprocessBuildWithReport
{
    public int callbackOrder
    {
        get { return 1; }
    }

    public void OnPostprocessBuild(BuildReport report)
    {
        Debug.Log($"=======UniversalLinkPostBuild OnPostprocessBuild start");

        if (report.summary.platform == BuildTarget.iOS)
        {
            AddAssociatedDomainsCapability(report.summary.outputPath);
            Debug.Log($"=======UniversalLinkPostBuild OnPostprocessBuild end");
        }
    }

    private static void AddAssociatedDomainsCapability(string path)
    {
        // Get the Xcode project path
        var projPath = PBXProject.GetPBXProjectPath(path);
        var proj = new PBXProject();
        proj.ReadFromFile(projPath);

        string entitlementsFileName = "Unity-iPhone.entitlements";
        // Add Associated Domains capability
        var manager = new ProjectCapabilityManager(
            projPath,
            entitlementsFileName,
            null,
            targetGuid: proj.GetUnityMainTargetGuid()
        );
        manager.AddAssociatedDomains(new string[]
        {
            "applinks:xxx.xxxx.xxxx",
        });
        manager.WriteToFile();
    }
}

Use your own applinks in xxx.xxxx.xxxx

Stormi answered 4/7 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.