Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity to iOS, info.plist Overwritten After PostProcessBuild

Background:

  1. Using Unity 2018.3.5f1 to build iOS Project
  2. Using PostProcessBuild Attribute to change info.plist
  3. Code worked in a different project in Unity 2018.2.1f1

I believe I'm doing what's suggested in this article: From Unity to iOS, how to perfectly automate frameworks, settings and plist?

I've confirmed that the PostBuildProcess function is called and the info.plist is changed. I log the output and it appears correct. I also saved a 2nd file in my code (info2.plist) and it contains all the changes I expect. However, the original file (info.plist) is overwritten with different values.

It seems that Unity is overwriting the info.plist file after the PostProcessBuild functions are running.

Code is placed in /Assets/Editor folder in Unity project.

Here's one of my functions:

[PostProcessBuild(1)]
public static void ChangeXcodePlist(BuildTarget buildTarget, string pathToBuiltProject)
{
    if (buildTarget == BuildTarget.iOS)
    {
        // Get plist file and read it.
        string plistPath = pathToBuiltProject + "/Info.plist";
        Debug.Log("In the ChangeXCodePlist, path is: " + plistPath);
        PlistDocument plist = new PlistDocument();
        plist.ReadFromString(File.ReadAllText(plistPath));
        Debug.Log("In the ChangeXCodePlist");

        // Get root
        PlistElementDict rootDict = plist.root;

        // Required when using camera for demos, e.g. AR demos.
        rootDict.SetString("NSCameraUsageDescription", "Uses the camera for Augmented Reality");

        // Required when using photo library in demo (i.e. reading library).
        rootDict.SetString("NSPhotoLibraryUsageDescription", "${PRODUCT_NAME} photo use");

        // Required when adding images to photo library in demos.
        rootDict.SetString("NSPhotoLibraryAddUsageDescription", "${PRODUCT_NAME} photo use");

        //ITSAppUsesNonExemptEncryption, this value is required for release in TestFlight.
        rootDict.SetBoolean("ITSAppUsesNonExemptEncryption", false);

        Debug.Log("PLIST: " + plist.WriteToString());

        // Write to file
        File.WriteAllText(plistPath, plist.WriteToString());
        File.WriteAllText(pathToBuiltProject + "/info2.plist", plist.WriteToString());
    }
}
like image 538
Philip Anthony Muse Avatar asked Mar 04 '23 09:03

Philip Anthony Muse


1 Answers

Thanks @dogiordano, that was the problem. A colleague added a library to the project that included a PostProcessBuild attribute with no calling order defined like this:

[PostProcessBuild]

whereas my 2 callbacks had the order specified:
[PostProcessBuild(0)]
[PostProcessBuild(1)]

There's nothing in the doc that describes what happens in this case: https://docs.unity3d.com/ScriptReference/Callbacks.PostProcessBuildAttribute.html

So, I tried my functions with (1) and (2) to see if the unspecified order would take the (0) slot and it does not. Like this:
[PostProcessBuild] - other library
[PostProcessBuild(1)] - mine
[PostProcessBuild(2)] - mine

So, if you don't specify a callback order, those functions are called after the functions that have an order specified.

I specified the callback order for all 3, and it works as expected.
[PostProcessBuild(0)] - other library
[PostProcessBuild(1)] - mine
[PostProcessBuild(2)] - mine

like image 185
Philip Anthony Muse Avatar answered Mar 09 '23 07:03

Philip Anthony Muse