Documentation Archive

Developer

Energy Efficiency Guide for Mac Apps

On This Page

Notify Your App When Active State Changes

As previously discussed, your app is automatically placed into a lower-powered state (App Nap) when certain criteria are met. However, you shouldn’t wait for the system to enact this measure. Your app can begin winding down activity once it’s notified that it’s about to become inactive (no longer frontmost), even if all or part of its window is still visible.

Implement Active App Transition Delegate Methods

Implement NSApplicationDelegate methods in your app delegate to receive calls when the active state of your app—whether it’s in the foreground—changes.

applicationWillResignActive

The applicationWillResignActive: method is called immediately before your app is no longer the foreground (frontmost) app, as shown in Listing 4-1. This is a good place to start winding down activity that can be stopped entirely once the change in state has completed.

Listing 4-1Preparing to stop operations before your app becomes inactive

Objective-C

  1. - (void)applicationWillResignActive:(NSNotification *)aNotification {
  2. // Prepare to halt operations, animations, and UI updates
  3. }

Swift

  1. func applicationWillResignActive(_ aNotification: NSNotification) {
  2. // Prepare to halt operations, animations, and UI updates
  3. })

applicationDidResignActive

The applicationDidResignActive: method is called immediately after your app gives up its position as the foreground app, as shown in Listing 4-2. Stop any power-intensive operations, animations, and UI updates to the extent possible.

Listing 4-2Stopping operations when your app becomes inactive

Objective-C

  1. - (void)applicationDidResignActive:(NSNotification *)aNotification {
  2. // Halt operations, animations, and UI updates
  3. }

Swift

  1. func applicationDidResignActive(_ aNotification: NSNotification) {
  2. // Halt operations, animations, and UI updates
  3. })

applicationWillBecomeActive

The applicationWillBecomeActive: method is called immediately before your app comes to the front, as shown in Listing 4-3. Start resuming operations.

Listing 4-3Preparing to resume operations before your app becomes active

Objective-C

  1. - (void)applicationWillBecomeActive:(NSNotification *)aNotification {
  2. // Prepare to resume operations, animations, and UI updates
  3. }

Swift

  1. func applicationWillBecomeActive(_ aNotification: NSNotification) {
  2. // Prepare to resume operations, animations, and UI updates
  3. })

applicationDidBecomeActive

The applicationDidBecomeActive: method is called immediately after your app becomes the frontmost app, as shown in Listing 4-4. Fully resume operations that were halted.

Listing 4-4Resuming operations when your app becomes active

Objective-C

  1. - (void)applicationDidBecomeActive:(NSNotification *)aNotification {
  2. // Resume operations, animations, and UI updates
  3. }

Swift

  1. func applicationDidBecomeActive(_ aNotification: NSNotification) {
  2. // Resume operations, animations, and UI updates
  3. })

Implement Active App Transition Notifications

In addition to implementing app transition delegate methods, register to receive notifications when changes in state occur by sending the message addObserver:selector:name:object: to the default notification center of your app (an instance of NSNotificationCenter). Pass it a selector to call and the name of the notification to receive. For changes in active state, register for any of the following notifications:

Listing 4-5 demonstrates how to register for a notification.

Listing 4-5Registering for an app transition notification

Objective-C

  1. [[NSNotificationCenter defaultCenter] addObserver:self
  2. selector:@selector(yourMethodName:)
  3. name:NSApplicationDidResignActiveNotification
  4. object:nil];
  5. )

Swift

  1. NSNotificationCenter.defaultCenter().addObserver(
  2. self,
  3. selector: "yourMethodName:",
  4. name: NSApplicationDidResignActiveNotification,
  5. object: nil
  6. )