Add Array of Strings to info.plist

I need some help. I need to add a completely new entry to info.plist. This new entry is an array and I need to set the first element to a value.

I have seen this post, but it only shows modifying an existing string in info.plist:

Specifically I am connecting to an external accessory and need to add the info.plist entry for:

Supported external accessory protocols

which is an array type.

then I need to set the first element to a protocol string similar to:

com.AmpedRFTech.Demo

I don’t need to copy splash screens or icons or any of that. All I need to do is add the above.

Can someone help me out?

I figured this out. Turns out it was easier than I thought. I assumed that since it was an Array type that it would be harder than just adding a string, but it isn’t. I also realized as I studied the existing code in the above link that the info.plist file is just XML. XML I know well.

There are a couple of things to note. When looking at the values in Xcode they are spelled out at strings like Support external accessory protocols. In the XML the key is actually UISupportedExternalAccessoryProtocols. I found this out by putting in what I thought it should be and it didn’t work. I then opened the info.plist file in MonoDevelop and it shows the real key string.

You will probably also notice that I needed to add the UIBackgroundModes array as well.

Hope this saves someone else some time.

Here is my script:

#!/usr/bin/perl
use File::Copy; 

print ("

*** Starting PostprocessBuildPlayer ***
");

my $installPath = $ARGV[0]; 
   
#go through the info.plist file line by line until you find this one: 
my $endOfPlist = "</dict>"; 
   
# The type of player built: 
# "dashboard", "standaloneWin32", "standaloneOSXIntel", "standaloneOSXPPC", "standaloneOSXUniversal", "webplayer", "iPhone" 
my $target = $ARGV[1]; 
   
print ("

*** PostprocessBuildPlayer - Building at ‘$installPath’ with target: $target ***
");

################################################################ 
# This modifies info.plist to add the external accessory       # 
# entries                                                      # 
################################################################ 
   
#open this file 
   
$oplistPath = $installPath."/Info.plist"; 
$nplistPath = $installPath."/Info.plist.tmp"; 
   
open OLDPLIST, "<", $oplistPath or die("Cannot open Info.plist"); 
open NEWPLIST, ">", $nplistPath or die("Cannot create new Info.plist"); 
   
my $nextLine = 0;    
       
while(<OLDPLIST>) 
{     
   ################################################################ 
   # Add any key/value pairs you want at the end of Info.plist    # 
   ################################################################ 
   
   if ($_ =~ m/$endOfPlist/) 
   { 
      my $keys = "";
          
      $keys .= "   <key>UISupportedExternalAccessoryProtocols</key>

";
$keys .= "
";
$keys .= " com.AmpedRFTech.Demo
";
$keys .= "
";

      $keys .= "   <key>UIBackgroundModes</key>

";
$keys .= "
";
$keys .= " App communicates with an accessory
";
$keys .= "
";

      $_ = $keys . $_; 
   } 
       
   print NEWPLIST $_; 
} 
   
close OLDPLIST; 
close NEWPLIST; 
   
`mv \'$nplistPath\' \'$oplistPath\'`;

Thanks to Hotshot10101 for the script. I modified it a bit. (for detecting the end of Info.plist correctly.)

#!/usr/bin/perl

use File::Copy;

print ("

*** Starting PostprocessBuildPlayer ***
");

my $installPath = $ARGV[0];

#go through the info.plist file line by line until you find this one:
my $endOfDict = "</dict>";
my $endOfPlist = "</plist>";

# The type of player built:
# "dashboard", "standaloneWin32", "standaloneOSXIntel", "standaloneOSXPPC", "standaloneOSXUniversal", "webplayer", "iPhone"
my $target = $ARGV[1];

print ("

*** PostprocessBuildPlayer - Building at ‘$installPath’ with target: $target ***
");

################################################################
# This modifies info.plist to add the external accessory       #
# entries                                                      #
################################################################

#open this file

$oplistPath = $installPath."/Info.plist";
$nplistPath = $installPath."/Info.plist.tmp";

open OLDPLIST, "<", $oplistPath or die("Cannot open Info.plist");
open NEWPLIST, ">", $nplistPath or die("Cannot create new Info.plist");

while(<OLDPLIST>)
{
    ################################################################
    # Add any key/value pairs you want at the end of Info.plist    #
    ################################################################

	if ($_ =~ m/$endOfDict/)
    {
    	my $pos = tell OLDPLIST;
    	my $nextLine = <OLDPLIST>;
    	if ($nextLine =~ m/$endOfPlist/)
    	{
	        my $keys = "";
	        
            $keys .= "   <key>UISupportedExternalAccessoryProtocols</key>

";
$keys .= "
";
$keys .= " com.AmpedRFTech.Demo
";
$keys .= "
";

            $keys .= "   <key>UIBackgroundModes</key>

";
$keys .= "
";
$keys .= " App communicates with an accessory
";
$keys .= "
";

	        print NEWPLIST $keys;
    	}
		seek OLDPLIST, $pos, 0;
    } 
    print NEWPLIST $_;
}

close OLDPLIST;
close NEWPLIST;

`mv \'$nplistPath\' \'$oplistPath\'`;