Converting an XML Plist to a Binary Plist

Converting an XML formatted plist into a binary one can be done programmatically in Swift using PropertyListSerialization:

   func xmlStringToBinaryData(plist:String) -> Data? {
       let data = plist.data(using: .utf8)
       var propertyListFormat = PropertyListSerialization.PropertyListFormat.xml
    
       do {
          let plistData = try PropertyListSerialization.propertyList(from: data!, options: .mutableContainersAndLeaves, format: &propertyListFormat)
    
          let output = try PropertyListSerialization.data(fromPropertyList: plistData, format: .binary, options: 0)
    
          return output
       } catch {
          return nil
       }
    }

Using plutil

If you just want to convert an existing XML plist file to a binary one, you can use Apple's own plutil command line utility. To use plutil, make sure you have downloaded the Xcode Command Line Tools.

To use the tool, open a Terminal window and type:
plutil -convert binary1 myplist.plist

You should now find a new binary plist file where your XML one was previously located.

Home Knowledge Base Support Download