Search Unity

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

Discussion in 'iOS and tvOS' started by _Adriaan, May 26, 2017.

  1. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    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:

    Code (CSharp):
    1.     <key>CFBundleIconFiles</key>
    2.     <array>
    3.       <string>AppIcon57x57.png</string>
    4.       <string>AppIcon57x57@2x.png</string>
    5.       <string>AppIcon60x60@2x.png</string>
    6.       <string>AppIcon72x72@2x~ipad.png</string>
    7.       <string>AppIcon72x72~ipad.png</string>
    8.       <string>AppIcon76x76@2x~ipad.png</string>
    9.       <string>AppIcon76x76~ipad.png</string>
    10.     </array>
    But... how do I remove these lines from the file?
     
  2. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    Ended up doing it the dirty way:

    Code (CSharp):
    1.  
    2.         var file = new List<string>(System.IO.File.ReadAllLines(plistPath));
    3.        
    4.         int removeStartIndex = -1;
    5.         int removeLineCount = -1;
    6.        
    7.         for (int i = 0; i < file.Count; i++)
    8.         {
    9.             if(removeStartIndex == -1)
    10.             {
    11.                 if(file[i].Contains("CFBundleIconFiles"))
    12.                 {
    13.                     removeStartIndex = i;
    14.                 }
    15.             }
    16.             else
    17.             {
    18.                 //on iOS, we're looking for the end of the array, while on tvOS
    19.                 // that array is empty marked with a tag with a slash at the end
    20.                 if(file[i].Contains("<array/>") || file[i].Contains("</array>"))
    21.                 {
    22.                     removeLineCount = i - removeStartIndex + 1; //+1 for current line
    23.                     break;
    24.                 }
    25.             }
    26.         }
    27.        
    28.         file.RemoveRange(removeStartIndex, removeLineCount);
    29.         System.IO.File.WriteAllLines(plistPath, file.ToArray());
    30.  
     
    Last edited: May 26, 2017
  3. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    To remove keys I do this:

    Code (CSharp):
    1. var plist = new PlistDocument();
    2. plist.ReadFromFile(path);
    3. var root = plist.root;
    4. var rootDic = root.values;
    5. rootDic.Remove(key);
     
    xaldin-76, jason_yak, Endahs and 4 others like this.
  4. _Adriaan

    _Adriaan

    Joined:
    Nov 12, 2009
    Posts:
    481
    Aaaahhhh I thought the getter root.values would return a copy of the Dict, but it returns a reference! Nice! Yeah, much better!!!