我可以将iOS Info.plist转换为ruby Hash吗?

时间:2015-07-31 21:40:18

标签: ios ruby hash plist

我需要在我的应用的Info.plist中排除特定的密钥,如果它们在另一个哈希中找到的话。目前我可以像这样访问Info.plist中的各个键

setting4Str = `/usr/libexec/PlistBuddy -c \"print :MySettingsDict:setting4" {settings_file}`

我可以在plist字典中的这个键上设置“setting 4 text”字符串...

但是,我希望能够遍历此MysettingsDict的所有键。有没有人有方法将iOS XML plist转换为ruby Dictionary?

<?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>CFBundleDisplayName</key>
    <string>My App</string>
    <key>CFBundleExecutable</key>
    <string>${EXECUTABLE_NAME}</string>
    <key>CFBundleIdentifier</key>
    <string>com.company.appname</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleLocalizations</key>
    <array>
        <string>en</string>
        <string>de</string>
        <string>it</string>
        <string>fr</string>
        <string>ru</string>
        <string>es</string>
    </array>
    <key>CFBundleName</key>
    <string>${PRODUCT_NAME}</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.3.2</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>com.company.mySchemeName</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>mySchemeName</string>
            </array>
        </dict>
    </array>
    <key>CFBundleVersion</key>
    <string>1.3.15.04170</string>
    <key>Internal version</key>
    <string>1.3.15.04170</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>MysettingsDict</key>
    <dict>
        <key>setting1</key>
        <false/>
        <key>setting2</key>
        <false/>
        <key>setting3</key>
        <false/>
        <key>setting4</key>
        <string>setting 4 text</string>
        <key>setting5</key>
        <string>setting 5 text</string>
        <key>setting6</key>
        <false/>
        <key>setting7</key>
        ...

3 个答案:

答案 0 :(得分:4)

为那些想要解析二进制 Info.plist的人添加。

info_plist = File.read("Info.plist") # read binary plist
IO.popen('plutil -convert xml1 -r -o - -- -', 'r+') {|f|
  f.write(info_plist)
  f.close_write
  info_plist = f.read # xml plist
}

现在info_plist是一个xml字符串。要快速提取一个属性,可以使用正则表达式:

app_bundle = info_plist.scan(/<key>CFBundleIdentifier<\/key>\s+<string>(.+)<\/string>/).flatten.first

parse xml and convert to hash.

答案 1 :(得分:2)

使用Nokogiri Gem解析xml,然后将其输出为ruby哈希值。

这是另一个stackoverflow answer,它提供了一个示例,如果文档不够。

您还可以使用Ruby Hash delete_ifhas_key?方法删除或允许您想要的任何内容。

答案 2 :(得分:0)

如果您正在寻找宝石,至少有两种选择:

  1. plist_lite
  2. plist

plist 是用纯 Ruby 编写的,而 plist_lite 是用 C 编写的。

如果您关心性能,请使用 plist_lite,它比 plist 快 5 倍以上。如果您需要一些丰富的功能,例如编码/解码非原始数据类型或格式,请使用 plist

plist_lite 的使用

require 'plist_lite'
plist = PlistLite.dump({foo: 'bar', ary: [1,2,3], time: Time.at(0)})
# => "<?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>foo</key><string>bar</string><key>ary</key><array><integer>1</integer><integer>2</integer><integer>3</integer></array><key>time</key><date>1970-01-01T00:00:00Z</date></dict></plist>"
PlistLite.load(plist)
# => {"foo"=>"bar", "ary"=>[1, 2, 3], "time"=>1970-01-01 00:00:00 UTC}

plist 的使用

result = Plist.parse_xml('path/to/example.plist')

result.class
=> Hash

"#{result['FirstName']} #{result['LastName']}"
=> "John Public"

result['ZipPostal']
=> "12345"