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.
Objective-C
- (void)applicationWillResignActive:(NSNotification *)aNotification {
// Prepare to halt operations, animations, and UI updates
}
Swift
func applicationWillResignActive(_ aNotification: NSNotification) {
// Prepare to halt operations, animations, and UI updates
})
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.
Objective-C
- (void)applicationDidResignActive:(NSNotification *)aNotification {
// Halt operations, animations, and UI updates
}
Swift
func applicationDidResignActive(_ aNotification: NSNotification) {
// Halt operations, animations, and UI updates
})
applicationWillBecomeActive
The applicationWillBecomeActive:
method is called immediately before your app comes to the front, as shown in Listing 4-3. Start resuming operations.
Objective-C
- (void)applicationWillBecomeActive:(NSNotification *)aNotification {
// Prepare to resume operations, animations, and UI updates
}
Swift
func applicationWillBecomeActive(_ aNotification: NSNotification) {
// Prepare to resume operations, animations, and UI updates
})
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.
Objective-C
- (void)applicationDidBecomeActive:(NSNotification *)aNotification {
// Resume operations, animations, and UI updates
}
Swift
func applicationDidBecomeActive(_ aNotification: NSNotification) {
// Resume operations, animations, and UI updates
})
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.
Objective-C
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yourMethodName:)
name:NSApplicationDidResignActiveNotification
object:nil];
)
Swift
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "yourMethodName:",
name: NSApplicationDidResignActiveNotification,
object: nil
)
Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-09-13