【问题标题】:How to automatically set associated domain for iOS app using Unity如何使用 Unity 为 iOS 应用程序自动设置关联域
【发布时间】:2017-11-11 03:09:27
【问题描述】:

我现在正在使用 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 unity3d unity5 ios-universal-links


    【解决方案1】:

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

    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 文档

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多