如何使用Unity自动为iOS应用程序设置关联域

问题描述 投票:0回答:3

我现在正在使用 Unity 开发一个 iOS 应用程序,我正在尝试设置关联的域,而无需在 xcode 上手动设置它。我相信我可以通过

PostProcessBuild
实现这一目标,但我无法弄清楚这一点

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);


    }
}
c# ios unity-game-engine ios-universal-links
3个回答
7
投票

我设法通过这样做添加关联的域

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();
}

不要忘记提供 .pbxproj 文件的路径,而不是 .plist 或项目文件本身的路径。
确保使用

WriteToFile()
应用更改。

有关 ProjectCapabilityManagerAddAssociatedDomains

的相关 Unity 文档

1
投票

我使用以下代码解决了问题:

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;
    }

0
投票

在Unity 2021.3中,将blew .cs添加到Editor文件夹

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();
    }
}

在 xxx.xxxx.xxxx 中使用您自己的应用链接

© www.soinside.com 2019 - 2024. All rights reserved.