Class

NSProcess​Info

The NSProcess​Info class provides methods that you use to access information about the current process. Each process has a single, shared NSProcess​Info object, known as a process information agent.

Overview

The process information agent can return information such as arguments, environment variables, host name, and process name. The process​Info class method returns the shared agent for the current process—that is, the process whose object sent the message. For example, the following line returns the NSProcess​Info object, which then provides the name of the current process:

NSString *processName = [[NSProcessInfo processInfo] processName];

The NSProcess​Info class also includes the operating​System method, which returns an enum constant identifying the operating system on which the process is executing.

NSProcess​Info objects attempt to interpret environment variables and command-line arguments in the user's default C string encoding if they cannot be converted to Unicode as UTF-8 strings. If neither the Unicode nor C string conversion works, these values are ignored by the NSProcess​Info object.

Managing Activities

The system has heuristics to improve battery life, performance, and responsiveness of applications for the benefit of the user. You can use the following methods to manage activities that give hints to the system that your application has special requirements:

In response to creating an activity, the system will disable some or all of the heuristics so your application can finish quickly while still providing responsive behavior if the user needs it.

You use activities when your application is performing a long-running operation. If the activity can take different amounts of time (for example, calculating the next move in a chess game), it should use this API. This will ensure correct behavior when the amount of data or the capabilities of the user's computer varies. You should put your activity into one of two major categories:

  1. User initiated: These are finite length activities that the user has explicitly started. Examples include exporting or downloading a user specified file.

  2. Background: These are finite length activities that are part of the normal operation of your application but are not explicitly started by the user. Examples include autosaving, indexing, and automatic downloading of files.

In addition, if your application requires high priority I/O, you can include the NSActivity​Latency​Critical flag (using a bitwise OR). You should only use this flag for activities like audio or video recording that really do require high priority.

If your activity takes place synchronously inside an event callback on the main thread, you do not need to use this API.

Be aware that failing to end these activities for an extended period of time can have significant negative impacts to the performance of your user's computer, so be sure to use only the minimum amount of time required. User preferences may override your application’s request.

You can also use this API to control automatic termination or sudden termination (see Sudden Termination). For example:

id activity = [[NSProcessInfo processInfo] beginActivityWithOptions:NSActivityAutomaticTerminationDisabled reason:@"Good Reason"];
// Perform some work
[[NSProcessInfo processInfo] endActivity:activity];

is equivalent to:

[[NSProcessInfo processInfo] disableAutomaticTermination:@"Good Reason"];
// Perform some work
[[NSProcessInfo processInfo] enableAutomaticTermination:@"Good Reason"];

Because this API returns an object, it may be easier to pair begins and ends than when using the automatic termination API—if the object is deallocated before the end​Activity:​ call, the activity will be automatically ended.

This API also provides a mechanism to disable system-wide idle sleep and display idle sleep. These can have a large impact on the user experience, so be sure not to forget to end activities that disable sleep (including NSActivity​User​Initiated).

Sudden Termination

macOS 10.6 and later includes a mechanism that allows the system to log out or shut down more quickly by, whenever possible, killing applications instead of requesting that they quit themselves.

Your application can enable this capability on a global basis and then manually override its availability during actions that could cause data corruption or a poor user experience by allowing sudden termination. Alternately, your application can just manually enable and disable this functionality.

The methods enable​Sudden​Termination and disable​Sudden​Termination decrement or increment, respectively, a counter whose value is 1 when the process is first created. When the counter's value is 0 the application is considered to be safely killable and may be killed by the system without any notification or event being sent to the process first.

Your application can support sudden termination upon launch by adding a key to the application’s Info.plist. If the NSSupports​Sudden​Termination key exists in the Info.plist and has a value of YES, it is the equivalent of calling enable​Sudden​Termination during your application launch. This renders the application process killable right away. You can still override this behavior by invoking disable​Sudden​Termination.

Typically, you disable sudden termination whenever your application defers work that must be done before the application terminates. If, for example, your application defers writing data to disk, and sudden termination is enabled, you should bracket the sensitive operations with a call to disable​Sudden​Termination, perform the necessary operations, and then send a balancing enable​Sudden​Termination message.

In agents or daemon executables that don't depend on AppKit you can manually invoke enable​Sudden​Termination right away. You can then use the enable and disable methods whenever the process has work it must do before it terminates.

Some AppKit functionality automatically disables sudden termination on a temporary basis to ensure data integrity.

  • NSUser​Defaults temporarily disables sudden termination to prevent process killing between the time at which a default has been set and the time at which the preferences file including that default has been written to disk.

  • NSDocument temporarily disables sudden termination to prevent process killing between the time at which the user has made a change to a document and the time at which the user's change has been written to disk.

Thermal State and App Performance in macOS

In macOS, use the current thermal state to determine if your app should reduce system usage. In macOS 10.10.3 and later, you can register for the NSProcess​Info​Thermal​State​Did​Change​Notification to be notified when the thermal state changes. Use thermal​State to query the current state. Your app should reduce system usage at higher thermal states. For recommended actions, see NSProcess​Info​Thermal​State.

Symbols

Getting the Process Information Agent

process​Info

Returns the process information agent for the process.

Accessing Process Information

arguments

Array of strings with the command-line arguments for the process.

environment

The variable names (keys) and their values in the environment from which the process was launched.

globally​Unique​String

Global unique identifier for the process.

process​Identifier

The identifier of the process (often called process ID).

process​Name

The name of the process.

Accessing User Information

Each user account in macOS has a full name (e.g., “Johnny Appleseed”) and an account name (e.g., “jappleseed”). You can view these names from the Users & Groups pane of System Preferences, and you can use either name to log in to your Mac.

user​Name

Returns the account name of the current user.

full​User​Name

Returns the full name of the current user.

Sudden Application Termination

- disable​Sudden​Termination

Disables the application for quickly killing using sudden termination.

- enable​Sudden​Termination

Enables the application for quick killing using sudden termination.

Controlling Automatic Termination

- disable​Automatic​Termination:​​

Disables automatic termination for the application.

- enable​Automatic​Termination:​​

Enables automatic termination for the application.

automatic​Termination​Support​Enabled

A Boolean value indicating whether the app supports automatic termination.

Getting Host Information

host​Name

The name of the host computer on which the process is executing.

- operating​System

Returns a constant to indicate the operating system on which the process is executing.

Deprecated
- operating​System​Name

Returns a string containing the name of the operating system on which the process is executing.

Deprecated
operating​System​Version​String

A string containing the version of the operating system on which the process is executing.

operating​System​Version

The version of the operating system on which the process is executing.

- is​Operating​System​At​Least​Version:​​

Returns a Boolean value indicating whether the version of the operating system on which the process is executing is the same or later than the given version.

Getting Computer Information

processor​Count

The number of processing cores available on the computer.

active​Processor​Count

The number of active processing cores available on the computer.

physical​Memory

The amount of physical memory on the computer in bytes.

system​Uptime

The amount of time the system has been awake since the last time it was restarted.

Managing Activities

- begin​Activity​With​Options:​​reason:​​

Begin an activity using the given options and reason.

- end​Activity:​​

Ends the given activity.

- perform​Activity​With​Options:​​reason:​​using​Block:​​

Synchronously perform an activity defined by a given block using the given options.

- perform​Expiring​Activity​With​Reason:​​using​Block:​​

Performs the specified block asynchronously and notifies you if the process is about to be suspended.

Getting the Thermal State

thermal​State

The current thermal state of the system.

Determining Whether Low Power Mode is Enabled

low​Power​Mode​Enabled

A Boolean indicating whether Low Power Mode is enabled on an iOS device.

Constants

NSProcess​Info​Thermal​State

The following constants are provided by the NSProcess​Info class as return values for thermal​State.

Anonymous

The following constants are provided by the NSProcess​Info class as return values for operating​System.

Deprecated

Notifications

NSProcess​Info​Thermal​State​Did​Change​Notification

Posted when the thermal state of the system changes.

NSProcess​Info​Power​State​Did​Change​Notification

Posted when the power state (Low Power Mode is enabled or disabled) of an iOS device changes.

Relationships

Inherits From