背景:xcode打包時,需要更改一些plist文件,以滿足不同的包要求
1、應用的Info.plist,更改包的版本號
2、導出包exportArchive的時候需要指定exportOptionsPlist的plist文件
當然我們可以通過python或shell等腳本去處理,但是比較麻煩,mac提供了PlistBuddy工具更編輯的操作plist文件
一、常見的plist操作
plist執行命令(具體支持的所有命令詳見第2部分):
/usr/libexec/PlistBuddy -c '命令 :命令的具體內容' xxx.plist
例如:/usr/libexec/PlistBuddy -c 'Add :provisioningProfiles:test1key string test1value' optionList.plist
例子初始化文件如下:

1、添加一個節點(使用Add命令)
a)普通類型
/usr/libexec/PlistBuddy -c 'Add :test_string_type string test_string_value' optionList.plist
說明:標紅部分string為值的類型,還可以添加bool等類型

b)字典類型
/usr/libexec/PlistBuddy -c 'Add :test_dict_type:key1 string value1' optionList.plist /usr/libexec/PlistBuddy -c 'Add :test_dict_type:key2 string value2' optionList.plist
說明:標紅部分由冒號分隔為兩部分,左側是字典的key,右側為字典的子項的key,最右側是對應子項的值

c)數組類型
/usr/libexec/PlistBuddy -c 'Add :test_list_type array' optionList.plist /usr/libexec/PlistBuddy -c 'Add :test_list_type: string value1' optionList.plist /usr/libexec/PlistBuddy -c 'Add :test_list_type: string value2' optionList.plist
說明:
1、數據類型需要先增加一個array類型的key,參見第一步
2、如果沒先建array的key,直接使用第2步添加,實際創建的是一個dict類型的key
2、修改節點內容(使用Set命令)
a)普通類型
/usr/libexec/PlistBuddy -c 'Set :test_string_type change_test_string_value' optionList.plist
說明:命令直接表示好key和value即可

b)更改字典的值
/usr/libexec/PlistBuddy -c 'Set :test_dict_type:key1 change_value1' optionList.plist

c)更改array數組的值
/usr/libexec/PlistBuddy -c 'Set :test_list_type:1 change_value2' optionList.plist
說明:key后數字表示要修改哪個子項(從0開始計算個數)

3、打印值(使用Print命令)
a)普通類型
/usr/libexec/PlistBuddy -c 'Print test_string_type' optionList.plist
b)數組類型
/usr/libexec/PlistBuddy -c 'Print test_list_type' optionList.plist /usr/libexec/PlistBuddy -c 'Print test_list_type:1' optionList.plist
說明:array的key后不加索引數字,打印所有列表值;加索引,打印對應位置的值(索引從0開始)

c)字典類型
/usr/libexec/PlistBuddy -c 'Print test_dict_type:key1' optionList.plist /usr/libexec/PlistBuddy -c 'Print test_dict_type' optionList.plist
說明:字典的key后面加上子項的key,會打印對應key的具體值。不指明打印的key值,直接顯示該字典所有值

4、刪除值(使用Delete命令)
a)普通類型
/usr/libexec/PlistBuddy -c 'Delete test_string_type ' optionList.plist
b)array類型
/usr/libexec/PlistBuddy -c 'Delete test_list_type:1 ' optionList.plist
說明:刪除整個array,參考刪除普通類型方法,直接刪除key
刪除array下的單個項,在arry的key后面加上【:索引值】
c)dict類型
/usr/libexec/PlistBuddy -c 'Delete test_dict_type:key1' optionList.plist /usr/libexec/PlistBuddy -c 'Delete test_dict_type' optionList.plist
說明:刪除整個array,參考刪除普通類型方法,直接刪除key
刪除字典下的某個key值,在字典key后面加上【:子項的key】
PlistBuddy還支持其他操作,比如復制、合並等操作,具體內容詳見下面第2部分的說明文檔
備注:如果要傳遞變量,需將變量用單引號括起來,例子如下
/usr/libexec/PlistBuddy -c 'Set :teamID '${teamId}'' optionList.plist
二、查看PlistBuddy支持的命令
/usr/libexec/PlistBuddy -help
支持命令如下:
Command Format: Help - Prints this information Exit - Exits the program, changes are not saved to the file Save - Saves the current changes to the file Revert - Reloads the last saved version of the file Clear [<Type>] - Clears out all existing entries, and creates root of Type Print [<Entry>] - Prints value of Entry. Otherwise, prints file Set <Entry> <Value> - Sets the value at Entry to Value Add <Entry> <Type> [<Value>] - Adds Entry to the plist, with value Value Copy <EntrySrc> <EntryDst> - Copies the EntrySrc property to EntryDst Delete <Entry> - Deletes Entry from the plist Merge <file.plist> [<Entry>] - Adds the contents of file.plist to Entry Import <Entry> <file> - Creates or sets Entry the contents of file Entry Format: Entries consist of property key names delimited by colons. Array items are specified by a zero-based integer index. Examples: :CFBundleShortVersionString :CFBundleDocumentTypes:2:CFBundleTypeExtensions Types: string array dict bool real integer date data Examples: Set :CFBundleIdentifier com.apple.plistbuddy Sets the CFBundleIdentifier property to com.apple.plistbuddy Add :CFBundleGetInfoString string "App version 1.0.1" Adds the CFBundleGetInfoString property to the plist Add :CFBundleDocumentTypes: dict Adds a new item of type dict to the CFBundleDocumentTypes array Add :CFBundleDocumentTypes:0 dict Adds the new item to the beginning of the array Delete :CFBundleDocumentTypes:0 dict Deletes the FIRST item in the array Delete :CFBundleDocumentTypes Deletes the ENTIRE CFBundleDocumentTypes array