You can get a notification when the system is about to power off, restart, or if the user just logs out. This is not an ordinary notification, but a workspace notification.
You can register for the notification like this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
//...more code...
self.powerOffRequestDate = [NSDate distantPast];
NSNotificationCenter *wsnCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
[wsnCenter addObserver:self
selector:@selector(workspaceWillPowerOff:)
name:NSWorkspaceWillPowerOffNotification
object:nil];
}
in the notification handler, you should just save away the date:
- (void)workspaceWillPowerOff:(NSNotification *)notification
{
self.powerOffRequestDate = [NSDate new];
}
Add
@property (atomic,strong,readwrite) NSDate *powerOffRequestDate;
to the appropriate place.
when your app is asked to terminate, you should get that date and check if the computer is about to shut off.
if([self.powerOffRequestDate timeIntervalSinceNow] > -60*5) {
// shutdown immediately
} else {
// ask user
}
I chose an intervall of 5minutes for the following edge case: The computer should power off, but another app cancels that. Your app is still running. 10min later, the user closes your app. In that case, the app should ask the user. This is a bit of a hack, but it's not a "crazy hack" I think...
Hope this helps.