macOS property list files

From Free Pascal wiki
macOSlogo.png

This article applies to macOS only.

See also: Multiplatform Programming Guide

Apple iOS new.svg

This article applies to iOS only.

See also: Multiplatform Programming Guide

Overview

A property list is a representation of a hierarchy of objects that can be stored in the file system and reconstituted later. Property lists give applications a lightweight and portable way to store small amounts of data. They are hierarchies of data made from specific types of objects—they are, in effect, an object graph. Property lists are easy to create programmatically and are even easier to serialize into a representation that is persistent. Applications can later read the static representation back into memory and recreate the original hierarchy of objects. Both Cocoa Foundation and Core Foundation have APIs related to property list serialization and deserialization.

Data types

Property lists consist only of certain types of data: dictionaries, arrays, strings, numbers (integer and float), dates, binary data, and Boolean values. Dictionaries and arrays are special types because they are collections; they can contain one or multiple data types, including other dictionaries and arrays. This hierarchical nesting of objects creates a graph of objects. The abstract data types have corresponding Foundation classes, Core Foundation types, and XML elements for collection objects and value objects.

Data formats

You can write property lists out in XML, JSON and binary formats. The binary format is much more compact than the XML version and thus more efficient. It is recommended for most situations. However, you can manually edit an XML property list if you ever need to. You can also edit a JSON file because it is just concatenated text and much less verbose than pure XML. Property list files have the filename extension of plist.

You should not use property lists to store large, complex graphs of objects, especially when the objects have variable mutability settings. And you cannot use property lists to store objects that are not supported by the architecture, such as model objects. For these cases, use archiving instead. Although property lists can include NSData objects, it’s best to not use data objects in property lists to hold large amounts of binary data.

XML, JSON or binary?

The property list file type can be identified using the file command on macOS:

 $ file org.sentry.picinfo.plist
 org.sentry.picinfo.plist: Apple binary property list           // binary format
 $ file org.sentry.picinfo.plist
 org.sentry.picinfo.plist: XML 1.0 document text, ASCII text    // XML format
 $ file org.sentry.picinfo.plist
 org.sentry.picinfo.plist: ASCII text, with no line terminators // JSON format

Converting between the different plist formats can be performed using the plutil command line utility on macOS:

 $ plutil -convert binary1 org.sentry.picinfo.plist   // convert to binary format
 $ plutil -convert xml1 org.sentry.picinfo.plist      // convert to XML format
 $ plutil -convert json org.sentry.picinfo.plist      // convert to JSON format

Info.plist

Every macOS and iOS application relies on the presence of special metadata in each application or bundle. This metadata is used in many different ways. Some of it is displayed to the user, some of it is used internally by the system to identify your application location, the icon to display, the document types it supports and many other behaviours that have an impact outside the bundle itself. Some of the metadata is used by the system frameworks to facilitate the launch of applications. The way an application provides its metadata to the system is through the use of a special file called an information property list file which is named Info.plist.

Creating an Info.plist

The simplest way to create an information property list file for as macOS application is to let Lazarus create it for you. When you use the Lazarus Project > Project Options, and click the Create application bundle option, Lazarus creates a default application bundle and an Info.plist file which you can find in the project_name.app/Contents subdirectory. The file created by Lazarus comes preconfigured with basic key value pairs that every information property list should have.

Here's an example of an Info.plist created by Lazarus for a simple macOS application:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>CFBundleDevelopmentRegion</key>
  <string>English</string>
  <key>CFBundleExecutable</key>
  <string>MyApp</string>
  <key>CFBundleName</key>
  <string>MyApp</string>
  <key>CFBundleIdentifier</key>
  <string>com.company.MyApp</string>
  <key>CFBundleInfoDictionaryVersion</key>
  <string>6.0</string>
  <key>CFBundlePackageType</key>
  <string>APPL</string>
  <key>CFBundleSignature</key>
  <string>MyAp</string>
  <key>CFBundleShortVersionString</key>
  <string>0.1</string>
  <key>CFBundleVersion</key>
  <string>1</string>
  <key>CSResourcesFileMapped</key>
  <true/>
  <key>CFBundleDocumentTypes</key>
  <array>
    <dict>
      <key>CFBundleTypeRole</key>
      <string>Viewer</string>
      <key>CFBundleTypeExtensions</key>
      <array>
        <string>*</string>
      </array>
      <key>CFBundleTypeOSTypes</key>
      <array>
        <string>fold</string>
        <string>disk</string>
        <string>****</string>
      </array>
    </dict>
  </array>
  <key>NSHighResolutionCapable</key>
  <true/>
</dict>
</plist>

Editing an Info.plist

To edit the contents of your Info.plist file, you can use any text editor that uses UTF-8 though it may be safer to use Xcode which understands the XML formatting of information property list files. Double-click the Info.plist filename in Finder which will automatically open the Xcode property list editor.

Note-icon.png

Note: The property list editor in Xcode displays human-readable strings (instead of the actual key name) for many keys by default. To display the actual key names as they appear in the Info.plist file, Control-click any of the keys in the editor window and enable the Show Raw Keys/Values item in the contextual menu.

To edit the value for a specify key, double-click the value in the Xcode property list editor to select it, then type a new value. Most values are specified as strings but Xcode also supports several other scalar types. You can also specify complex types such as an array or dictionary. The property list editor displays an appropriate interface for editing each type. To change the type of a given value, make sure the value is not selected and Control-click it to display its contextual menu. From the Value Type submenu, select the type you want to use for the value.

Adding keys to an Info.plist

Although the Info.plist file generated by Lazarus contains the most critical keys required by the system, most applications should typically specify several additional keys. Many subsystems and system applicationss use the Info.plist file to gather information about your application. For example, when the user chooses File > Get Info for your application, the Finder displays information from many of these keys in the resulting information window.

To add a key/value pair:

  • Click the Add button (+) beside a key in the property list editor or select an existing property and press Return.
  • Choose a key from the pop-up menu (press the Down Arrow key to display it if it’s not visible) or type a new key name in the Key column.
  • Choose a type from the pop-up menu in the Type column.
  • Enter a value in the Value column.

To add a value to an array or dictionary, expand the disclosure triangle beside the array or dictionary. Next, click the Add button (+) or press Return to add a child property.

To delete a key/value pair:

  • Click the Remove button (—) beside a key in the property list editor or select a property and press Delete.

Syntax checking

In cases where you have edited a property list file by hand rather than by using Xcode's property list editor, it is prudent to syntax check the file using the plutil command line utility on macOS. This is very handy because plutil will tell you the number of the line on which it finds an error.

A successful syntax check:

 $ plutil Info.plist
 Info.plist: OK
 

An syntax check where a problem has been found:

 $ plutil Info.plist
 Info.plist: Found non-key inside <dict> at line 40

Of course plutil will syntax check any property list file, not just Info.plist.

Recommended Key/Value pairs

iOS

An iOS application should include the following keys in its information property list file:

  • CFBundleDevelopmentRegion
  • CFBundleDisplayName
  • CFBundleExecutable
  • CFBundleIconFiles
  • CFBundleIdentifier
  • CFBundleInfoDictionaryVersion
  • CFBundlePackageType
  • CFBundleVersion
  • LSRequiresIPhoneOS

In addition to these keys, there are several that are commonly included:

  • UIRequiredDeviceCapabilities (required)
  • UIStatusBarStyle
  • UIInterfaceOrientation
  • UIRequiresPersistentWiFi

macOS Cocoa

A macOS Cocoa application should include the following keys in its information property list file. Most are set by Lazarus automatically when you create your application bundle, but some will need to be edited and some may need to be added.

  • CFBundleDevelopmentRegion
  • CFBundleDisplayName
  • CFBundleExecutable
  • CFBundleIconFile
  • CFBundleIdentifier
  • CFBundleInfoDictionaryVersion
  • CFBundleName
  • CFBundlePackageType
  • CFBundleShortVersionString
  • CFBundleVersion
  • NSHumanReadableCopyright

Localizing an Info.plist

The values for many keys in an information property list file are human-readable strings that are displayed to the user by the Finder or your own app. When you localize your app, you should be sure to localize the values for these strings in addition to the rest of your app’s content.

Localized values are not stored in the Info.plist file itself. Instead, you store the values for a particular localization in a strings file with the name InfoPlist.strings. You place this file in the same language-specific project directory that you use to store other resources for the same localization. The contents of the InfoPlist.strings file are the individual keys you want localized and the appropriately translated value. The routines that look up key values in the Info.plist file take the user’s language preferences into account and return the localized version of the key (from the appropriate InfoPlist.strings file) when one exists. If a localized version of a key does not exist, the routines return the value stored in the Info.plist file.

In addition to the recommended keys, there are several keys that should be localized and placed in your language-specific InfoPlist.strings files:

  • CFBundleDisplayName
  • CFBundleName
  • CFBundleShortVersionString
  • NSHumanReadableCopyright

For example, TextEdit has several keys that are displayed in the Finder and thus should be localized. Suppose your information property list file defines the following keys:

<key>CFBundleDisplayName</key>
<string>TextEdit</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 1995-2009, Apple Inc.,All Rights Reserved.
</string>

The French localization for TextEdit then includes the following strings in the InfoPlist.strings file of its Contents/Resources/French.lproj directory:

CFBundleDisplayName = "TextEdit";
NSHumanReadableCopyright = "Copyright © 1995-2009 Apple Inc.\nTous droits réservés.";

See also

External links