iOS Reference Library Apple Developer
Search

Environment Variables

Environment variables are another way to configure your application dynamically. Many applications and systems use environment variables to store important information, such as the location of executable programs and header files. The variable consists of a key string with the name of the variable and a value string.

To get the value of an environment variable, your application must call the getenv function that is part of the standard system library (stdlib.h). You pass this function a string containing the name of the variable you want and it returns the value, or NULL if no variable of that name was found. Your application can then use the variable as it sees fit.

Environment Variable Scope

Environment variables are scoped to the process that created them and to any children of that process. The Terminal application treats each window as its own separate process for the sake of managing environment variables. Thus, if you create a Terminal window and define some environment variables, any programs you execute from that window inherit those variables. However, you cannot access the variables defined in the first window from a second Terminal window, and vice versa.

Sessions can be inherited. For example, when a user logs in, the system creates a user session and defines a standard set of environment variables. Any processes launched by the user during the session inherit the user environment variables. However, this inheritance is a read-only relationship. Any changes made to the variable by a process remain local to that process and are not inherited by other processes.

User Session Environment Variables

Mac OS X supports the definition of environment variables in the scope of the current user session. On login, the loginwindow application looks for a special property list file with the name environment.plist. This file must be located in a directory called .MacOSX at the root of the user’s home directory. The path to this file (which you must create) is as follows:

~/.MacOSX/environment.plist

If an environment.plist file exists, loginwindow looks for keys that are children of the root element. For each of these keys, loginwindow registers an environment variable of the same name and assigns it the value of the key. This file supports only the definition of environment variables. You cannot use this file to execute other forms of script code. The format of the file is the same XML format as other property list files, with each key in the file containing a string value. For example, in the Property List Editor application (located in <Xcode>/Applications/Utilities, where <Xcode> is your Xcode installation directory), such a property-list file might look like the following:

image: Art/environment_plist.gif

Application-Specific Environment Variables

There are two ways to make environment variables available to an application. The first is to define the variables in a Terminal session and then launch the application from the same session. When launched from Terminal, the application inherits the session settings, including any environment variables defined there.

The second way to associate environment variables with an application is to include the LSEnvironment key in the application’s information property list file. The LSEnvironment key lets you specify an arbitrary number of key/value pairs representing environment variables and their values. Because it requires modifying the application’s information property list file, use of this key is best for options that do not change too frequently. For more information on using this key, see Information Property List Key Reference.




Last updated: 2009-10-19

Did this document help you? Yes It's good, but... Not helpful...