Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I make plist content programatically?

This is my plist content in below, is there a way to make this content with code? instead just having it like as a string?

let test = """
<?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>Label</key>
          <string>com.yourdomain.onstartup</string>
          <key>LimitLoadToSessionType</key>
          <string>Aqua</string>
          <key>Program</key>
          <string>/Applications/On Startup.app/Contents/MacOS/On Startup</string>
          <key>RunAtLoad</key>
          <true/>
</dict>
</plist>
"""

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import Foundation

let plist: [String: Any] = [
    "Label": "com.yourdomain.onstartup",
    "LimitLoadToSessionType": "Aqua",
    "Program": "/Applications/On Startup.app/Contents/MacOS/On Startup",
    "RunAtLoad": true
]

let data = try! PropertyListSerialization.data(
    fromPropertyList: plist,
    format: .xml,
    options: 0
)
let string = String(decoding: data, as: UTF8.self)
print(string)

Output:

<?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>Label</key>
    <string>com.yourdomain.onstartup</string>
    <key>LimitLoadToSessionType</key>
    <string>Aqua</string>
    <key>Program</key>
    <string>/Applications/On Startup.app/Contents/MacOS/On Startup</string>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading