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

jcp*_*her 1 ruby hash plist ios

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

setting4Str = `/usr/libexec/PlistBuddy -c \"print :MySettingsDict:setting4" {settings_file}`
Run Code Online (Sandbox Code Playgroud)

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

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

<?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>
        ...
Run Code Online (Sandbox Code Playgroud)

Rom*_* B. 6

添加给那些想要解析二进制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
}
Run Code Online (Sandbox Code Playgroud)

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

app_bundle = info_plist.scan(/<key>CFBundleIdentifier<\/key>\s+<string>(.+)<\/string>/).flatten.first
Run Code Online (Sandbox Code Playgroud)

或者解析xml并转换为hash。