Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

PBXProject fails to apply iCloud capabilities in 2019.4

Discussion in 'iOS and tvOS' started by Freaking-Pingo, Jan 4, 2021.

  1. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    We are trying to add the iCloud capabilities to our xcode project as part of our post processing for builds, but it doesn't work. This is our code:

    Code (CSharp):
    1.  
    2. public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
    3.     {
    4.         if (target != BuildTarget.iOS)
    5.             return;
    6.         var fullProjectPath = pathToBuiltProject + XCODE_PROJ;
    7.         //Get the Xcode project
    8.         var project = new PBXProject();
    9.         project.ReadFromString(File.ReadAllText(fullProjectPath));
    10. #if UNITY_2019_3_OR_NEWER
    11.         string targetGuid = project.GetUnityFrameworkTargetGuid();
    12. #else
    13.         string targetGuid = project.TargetGuidByName("Unity-iPhone");
    14. #endif
    15.         project.AddFrameworkToProject(targetGuid, "WebKit.framework", false);
    16.         project.AddFrameworkToProject(targetGuid, "MobileCoreServices.framework", false);
    17.         project.AddFrameworkToProject(targetGuid, "CloudKit.framework", false);
    18.         project.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-ObjC");
    19.         project.AddBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");
    20.  
    21.         var mainTarget = project.GetUnityMainTargetGuid();
    22.         var capabilityManager = new ProjectCapabilityManager(fullProjectPath, "Unity-iPhone/trainingplatform.entitlements", targetGuid: target);
    23.         capabilityManager.AddGameCenter(); //Works fine
    24.         capabilityManager.AddiCloud(false, true, null); //Why U No Work?
    25.         capabilityManager.WriteToFile();
    26.  
    27.         //Write the changes
    28.         File.WriteAllText(fullProjectPath, project.WriteToString());
    29.     }
    30.  
    When we look into the .entitlement file that is produced, we noticed that Unity produces a different .entitlement file than the .entitlement file produced when you manually add the iCloud capabilities.

    This is the one created by Unity:
    Code (CSharp):
    1.  <dict>
    2.   <key>com.apple.developer.icloud-container-identifiers</key>
    3.   <array>
    4.    <string>iCloud.$(CFBundleIdentifier)</string>
    5.   </array>
    6.   <key>com.apple.developer.icloud-services</key>
    7.   <array>
    8.    <string>CloudDocuments</string>
    9.    <string>CloudKit</string>
    10.   </array>
    11.   <key>com.apple.developer.ubiquity-container-identifiers</key>
    12.   <array>
    13.    <string>iCloud.$(CFBundleIdentifier)</string>
    14.   </array>
    15. </dict>
    This is the one created by xcode itself:

    Code (CSharp):
    1. <dict>
    2.     <key>aps-environment</key>
    3.     <string>development</string>
    4.     <key>com.apple.developer.icloud-container-identifiers</key>
    5.     <array>
    6.         <string>iCloud.someCompany.someApp</string>
    7.     </array>
    8.     <key>com.apple.developer.icloud-services</key>
    9.     <array>
    10.         <string>CloudKit</string>
    11.     </array>
    12. </dict>
    We also noticed a few different lines were modified in the project.pbxproject file when adding the capabilities manually. This is a diff of the project.pbxproject file when manually adding the iCloud capabilities in the xcode project generated by Unity. (red = removed, green = added)



    So our question is, why doesn't the above code work for adding iCloud capabilities? We believe its related to Unity producing a different generated result from when you manually add iCloud. We are not certain if this is related, but we found another thread with other developers having trouble with adding capabilities, believing it is related to the shift in xcode structure that occured in 2019.4.

    Link to thread: https://forum.unity.com/threads/pro...napppurchase-not-working.940893/#post-6387195
     
  2. Freaking-Pingo

    Freaking-Pingo

    Joined:
    Aug 1, 2012
    Posts:
    310
    We ended up solving the problem :)

    Apparently the order which you save the ProjectCapabilityManager and the PBXProject is important. In my above example we saved the ProjectCapabilityManager first using capabilityManager.WriteToFile(); then we would save the PBXProject using File.WriteAllText(fullProjectPath, project.WriteToString());. What would happen is that our changes to the ProjectCapabilityManager was overwritten when we saved the PBXProject. Switching them around, saving the PBXProject first and then saving the ProjectCapabilityManager did the trick
     
    kyubuns likes this.