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

C# 如何使用Unity自动设置iOS应用程序的关联域,c#,ios,unity3d,unity5,ios-universal-links,C#,Ios,Unity3d,Unity5,Ios Universal Links,我现在正在使用Unity开发一个iOS应用程序,我正在尝试设置关联的域,而无需在xcode上手动设置它。我相信我可以通过后处理构建实现这一点,但我无法理解这一点 public class AvametricIOSBuildPostProcessor { static string[] associatedDomains; [PostProcessBuild] public static void OnPostprocessBuild (BuildTarget targe

我现在正在使用Unity开发一个iOS应用程序,我正在尝试设置关联的域,而无需在xcode上手动设置它。我相信我可以通过
后处理构建
实现这一点,但我无法理解这一点

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


    }
}

通过这样做,我成功地添加了一个关联域

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()
应用更改


关于和的相关Unity文档

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

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;
    }
private static bool addPushAndAssociatedDomainCapability(PBXProject项目,字符串路径ToBuildProject)
{
var targetName=PBXProject.GetUnityTargetName();
var targetGuid=project.TargetGuidByName(targetName);
var productName=project.GetBuildPropertyForAnyConfig(targetGuid,“产品名称”);
var fileName=productName+“.authentications”;
var authorizefilepath=pathToBuildProject+“/”+targetName+“/”+fileName;
if(File.Exists(AuthenticationFilePath)==true)
{
Log(“[Builder]授权文件存在”);
返回true;
}
bool success=project.AddCapability(targetGuid、PBXCapabilityType.PushNotifications);
if(success==false)
{
Log(“[Builder]打开推送功能失败”);
返回false;
}
success=project.AddCapability(targetGuid,PBXCapabilityType.AssociatedDomains);
常量字符串权限=@“
aps环境
发展
com.apple.developer.associated-domains
xxx
";
尝试
{
writealText(授权文件路径,授权);
project.AddFile(targetName+“/”+文件名,文件名);
project.AddBuildProperty(targetGuid,“代码签名权利”,targetName+“/”+文件名);
}
捕获(IOE异常)
{
Debug.LogError(“无法复制权限。可能已经存在。”+e);
}
Log(“[Builder]添加推送功能成功。”);
返回true;
}