Removing keys from Info.plist (instead of adding them)

Hey there!

Using Unity’s convenient plist class, you can easily add and modify keys and values in the Info.plist, but there doesn’t seem to be a way to remove keys. I need to remove the following lines when compiling for iOS 9.0 to not get any warnings from iTunes later when submitting, because they are deprecated / obsolete since iOS 9.0:

    <key>CFBundleIconFiles</key>
    <array>
      <string>AppIcon57x57.png</string>
      <string>AppIcon57x57@2x.png</string>
      <string>AppIcon60x60@2x.png</string>
      <string>AppIcon72x72@2x~ipad.png</string>
      <string>AppIcon72x72~ipad.png</string>
      <string>AppIcon76x76@2x~ipad.png</string>
      <string>AppIcon76x76~ipad.png</string>
    </array>

But… how do I remove these lines from the file?

Ended up doing it the dirty way:

        var file = new List<string>(System.IO.File.ReadAllLines(plistPath));
       
        int removeStartIndex = -1;
        int removeLineCount = -1;
       
        for (int i = 0; i < file.Count; i++) 
        {
            if(removeStartIndex == -1)
            {
                if(file[i].Contains("CFBundleIconFiles"))
                {
                    removeStartIndex = i;
                }
            }
            else
            {
                //on iOS, we're looking for the end of the array, while on tvOS
                // that array is empty marked with a tag with a slash at the end
                if(file[i].Contains("<array/>") || file[i].Contains("</array>"))
                {
                    removeLineCount = i - removeStartIndex + 1; //+1 for current line
                    break;
                }
            }
        }
       
        file.RemoveRange(removeStartIndex, removeLineCount);
        System.IO.File.WriteAllLines(plistPath, file.ToArray());

To remove keys I do this:

var plist = new PlistDocument();
plist.ReadFromFile(path);
var root = plist.root;
var rootDic = root.values;
rootDic.Remove(key);
7 Likes

Aaaahhhh I thought the getter root.values would return a copy of the Dict, but it returns a reference! Nice! Yeah, much better!!!