Search Unity

How to add entitlement entry ?

Discussion in 'iOS and tvOS' started by pistoleta, Jul 24, 2021.

  1. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    539
    I need to add this:

    And I would like to be able to set it up from unity so I dont have to modify the Entitlements file every time we build.
    I just found information about adding capabilities but nothing about adding entries to the entitlements file.
    Is simply not possible or am I missing something?

    Thanks in advance!
     
  2. lupidan

    lupidan

    Joined:
    Jan 14, 2014
    Posts:
    47
    Option A) ProjectCapabilityManager and private method
    An entitlements file is just a plist file with the .entitlement extension.

    You could use "ProjectCapabilityManager", and some reflection to get a private method, in a post build processor. If the private method were to change in the future, this would need to be reworked for the new implementation.

    Code (CSharp):
    1. // Create a ProjectCapabilityManager
    2. // path is the xcode path received in the post processor
    3. var entitlementsFilePath = "Entitlements.entitlements";
    4. var projectPath = PBXProject.GetPBXProjectPath(path);
    5. var project = new PBXProject();
    6. project.ReadFromString(System.IO.File.ReadAllText(projectPath));
    7. var manager = new ProjectCapabilityManager(projectPath, entitlementsFilePath, null, project.GetUnityMainTargetGuid());
    8.  
    9. // Get the hidden GetOrCreateEntitlementDoc method
    10. var managerType = typeof(ProjectCapabilityManager);
    11. var getOrCreateEntitlementDocMethod = managerType.GetMethod("GetOrCreateEntitlementDoc", NonPublicInstanceBinding);
    12. var entitlementDoc = getOrCreateEntitlementDocMethod.Invoke(manager, new object[] { }) as PlistDocument;
    13.  
    14. // Update the entilement Doc with whatever you want to add to the plist document
    15.  
    16. // Write the file(s)
    17. manager.WriteToFile()
    Option B) Complex, but "more correct"


    We will make a Post Processing build script. That post processing receives a path to the exported Xcode project:

    1. Load the Xcode project into a PBXProject instance, and get the main target.
    Code (CSharp):
    1. // path is the xcode path received in the post processor
    2. var projectPath = PBXProject.GetPBXProjectPath(path);
    3. var project = new PBXProject();
    4. project.ReadFromString(System.IO.File.ReadAllText(projectPath));
    5. var mainTargetGuid = project.GetUnityMainTargetGuid();
    2. Get the value for the entitlements file path in the Xcode project. For that you need to look at a specific build property on the main target. It could be empty if no entitlements have been generated yet
    Code (CSharp):
    1. var entitlementsFilePath = project.GetBuildPropertyForAnyConfig(mainTargetGuid, "CODE_SIGN_ENTITLEMENTS");
    3. Check if a entitlements file already exists in that file path. If the file does not exist, create a PlistDocument from zero. If the file exists, load it into a PlistDocument. The entitlements file is a Plist file with the .entitlement extension.

    4. Modify the entitlements file to your liking, and write the file into the specific entitlements file path. If your script created the file for the first time, you can specify the file path you want. Normally it should stay at the root of the Xcode project.

    5. If you created the entitlements file yourself, make sure you set the build property for any config with the filepath you created, and write the PBXProject into the same xcode project file!!!
    Code (CSharp):
    1. project.SetBuildProperty(mainTargetGuid, "CODE_SIGN_ENTITLEMENTS", yourEntitlementsFilepath);
    2. project.WriteToFile(projectPath);
    Whether the entitlements file and information are present or not during the execution of your script depends on other post processing scripts in your project, and the order in which they are executed
     
    a436t4ataf and pistoleta like this.
  3. pistoleta

    pistoleta

    Joined:
    Sep 14, 2017
    Posts:
    539

    Thanks a lot, I took the second solution. Also I was failing to realize I just had to manipulate the entitlements file as a Plist document.
     
    lupidan likes this.
  4. BurningthumbStudios

    BurningthumbStudios

    Joined:
    Apr 28, 2008
    Posts:
    95
    a436t4ataf likes this.