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

时间:2017-11-11 03:09:28

标签: c# ios unity3d unity5 ios-universal-links

我正在使用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);


    }
}

2 个答案:

答案 0 :(得分:0)

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

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 :(得分:0)

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

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