前言
defaults、plutil、PlistBuddy的差別:
- defaults指令使用domain的概念通路plist檔案,而且對指令的檔案書寫方式要求嚴格(比如不能帶.plist),有的時候還找不到或無法通路非标準目錄的,而且對複雜格式資料的鞋操作比較麻煩,但是它的讀取操作确比plutil簡單。
#顯示系統隐藏檔案
defaults write ~/Library/Preferences/com.apple.finder AppleShowAllFiles - bool true
- plutil對于複雜格式的寫操作很直覺,很類似于對類的操作,而且檔案名也是直截了當,就是讀資料功能太弱。
- PlistBuddy可以說具有前面兩者的優點,避免了它們各自的缺點,書寫也友善。
但是對于系統參數的plist檔案操作,還是使用Defaults指令号,因為它會通知系統服務該參數檔案已經修改,而PlistBuddy則不會。
I Info.plist的常用配置
1.1 支援微信喚起支付
canOpenURL: failed for URL: weixin://app/wx的解決方案
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
</array>
1.2 支援通路相冊進行圖檔儲存
<key>NSPhotoLibraryAddUsageDescription</key>
<string></string>
1.3 支援通路照相機 NSCameraUsageDescription
<key>NSCameraUsageDescription</key>
<string></string>
1.4 ATS
從iOS 9開始,所有使用NSURLSession建立的HTTP連接配接預設使用ATS(App Transport Security),這個新的安全協定要求所有的網絡連接配接必須使用HTTPS協定,否則會提示如下錯誤。
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
如果有一些特殊的原因需要忽略ATS限制的話,可以在Info.plist檔案中添加如下配置。
NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
II 使用macOS自帶的PlistBuddy進行CURD(增删改查)plist檔案
read and write values
- ➜ retail git:(develop) man PlistBuddy
SYNOPSIS
PlistBuddy [-cxh] file.plist
-rwxr-xr-x 1 root wheel 54256 4 7 04:45
- 應用場景: 在修改tweak的plist的時候,如果要臨時調試修改修改plist檔案,或者使用腳本修改plist内容的時候,使用PlistBuddy就很友善
2.1 建立plist
➜ Desktop /usr/libexec/PlistBuddy test.plist
File Doesn't Exist, Will Create: test.plist
Command: save
Saving...
Command: exit
➜ Desktop ls -lrt | grep test.plist
-rw-r--r-- 1 mac staff 181 9 29 16:01 test.plist
➜ Desktop cat test.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>
如果不想每次都使用全全路徑使用PlistBuddy,可以在 .bash_profile配置alias
➜ ~ open -e .bash_profile
内容如下
alias PlistBuddy='/usr/libexec/PlistBuddy'
記得source ~/.bash_profile,才會在目前終端生效。
2.2 添加key
2.2.1 添加基本類型的key Add :字段名 類型 值
-
PlistBuddy -c 'Add :Version string 1.0' test.plist
➜ Desktop PlistBuddy -c 'Add :Version string 1.0' test.plist
➜ Desktop cat test.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Version</key>
<string>1.0</string>
</dict>
</plist>
2.2.2 array類型的key
先使用
Add :數組名 array
設定數組名,在使用
Add :數組名: 類型 值
添加數組元素
➜ Desktop PlistBuddy -c 'Add :Applications array' test.plist
➜ Desktop PlistBuddy -c 'Add :Applications: string app1' test.plist
➜ Desktop PlistBuddy -c 'Add :Applications: string app2' test.plist
➜ Desktop PlistBuddy -c "print" test.plist
Dict {
Users = Dict {
Name = Bob
sex = false
Age = 20
}
Version = 1.0
2.2.3 dict類型的key
- 先添加字典名
Add :字典名 dict
- 再添加字典的元素:key和value
Add :字典名:key 類型 值
➜ Desktop PlistBuddy -c 'Add :Users dict' test.plist
➜ Desktop PlistBuddy -c 'Add :Users:Name string Bob' test.plist
➜ Desktop PlistBuddy -c 'Add :Users:sex Bool ture' test.plist
➜ Desktop PlistBuddy -c 'Add :Users:Age integer 20' test.plist
➜ Desktop PlistBuddy -c "print" test.plist
Dict {
Users = Dict {
Name = Bob
sex = false
Age = 20
}
Version = 1.0
}
➜ Desktop cat test.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Users</key>
<dict>
<key>Age</key>
<integer>20</integer>
<key>Name</key>
<string>Bob</string>
<key>sex</key>
<false/>
</dict>
<key>Version</key>
<string>1.0</string>
</dict>
</plist>
2.3 檢視plist
-
PlistBuddy -c "print" test.plist
-
至于檢視plist,個人更新後plutil的json格式輸出,因為可以明顯看出字段的類型plutil -p
➜ Desktop plutil -p test.plist
{
"Applications" => [
0 => "app1"
1 => "app2"
]
"Users" => {
"Age" => 20
"Name" => "Bob"
"sex" => 0
}
"Version" => "1.0"
-
: 标準的XML文本格式cat test.plist
2.3.1 根據key查value
➜ Desktop PlistBuddy -c 'Print :Users' test.plist
Dict {
Name = Bob
sex = false
2.3.2 根據下标查數組的元素
➜ Desktop PlistBuddy -c 'Print :Applications:1'
2.4 修改
➜ Desktop PlistBuddy -c 'Set :Applications:1 "reset application 1"' test.plist
➜ Desktop PlistBuddy -c 'Print :Applications:1' test.plist
reset application 1
2.5 删除
➜ Desktop PlistBuddy -c 'Delete :Applications' test.plist
➜ Desktop PlistBuddy -c "print" test.plist
Dict {
Users = Dict {
Name = Bob
sex = false
Age = 20
}
Version = 1.0
2.6 合并
➜ Desktop PlistBuddy -c 'print' test2.plist
Dict {
kunnanVersion = 9.0
}
➜ Desktop PlistBuddy -c 'print' test.plist
Dict {
Users = Dict {
Name = Bob
sex = false
Age = 20
}
Version = 1.0
}
➜ Desktop PlistBuddy -c 'Merge test2.plist' test.plist
➜ Desktop PlistBuddy -c 'print' test.plist
Dict {
Users = Dict {
Name = Bob
sex = false
Age = 20
}
Version = 1.0
kunnanVersion = 9.0