- Home /
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:
http://www.unifycommunity.com/wiki/index.php?title=IPhone_Build_Utility_Script
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?
Answer by Hotshot10101 · Jan 18, 2013 at 06:27 PM
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 ("\n*** Starting PostprocessBuildPlayer ***\n");
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 ("\n*** PostprocessBuildPlayer - Building at '$installPath' with target: $target ***\n");
################################################################
# 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>\n";
$keys .= " <array>\n";
$keys .= " <string>com.AmpedRFTech.Demo</string>\n";
$keys .= " </array>\n";
$keys .= " <key>UIBackgroundModes</key>\n";
$keys .= " <array>\n";
$keys .= " <string>App communicates with an accessory</string>\n";
$keys .= " </array>\n";
$_ = $keys . $_;
}
print NEWPLIST $_;
}
close OLDPLIST;
close NEWPLIST;
`mv \'$nplistPath\' \'$oplistPath\'`;
Answer by sopepos · May 07, 2015 at 09:15 AM
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 ("\n*** Starting PostprocessBuildPlayer ***\n");
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 ("\n*** PostprocessBuildPlayer - Building at '$installPath' with target: $target ***\n");
################################################################
# 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>\n";
$keys .= " <array>\n";
$keys .= " <string>com.AmpedRFTech.Demo</string>\n";
$keys .= " </array>\n";
$keys .= " <key>UIBackgroundModes</key>\n";
$keys .= " <array>\n";
$keys .= " <string>App communicates with an accessory</string>\n";
$keys .= " </array>\n";
print NEWPLIST $keys;
}
seek OLDPLIST, $pos, 0;
}
print NEWPLIST $_;
}
close OLDPLIST;
close NEWPLIST;
`mv \'$nplistPath\' \'$oplistPath\'`;
Your answer
Follow this Question
Related Questions
Inventory system using an array... 3 Answers
Increasing And Decreasing An Array Through A Variable 2 Answers
A node in a childnode? 1 Answer