NSWorkspaceWillPowerOffNotification never called

前提是你 提交于 2019-12-12 11:30:13

问题


I am trying to run a program in a background process that will register every shutdown event in the system.

Doing so by registering to NSWorkspaceWillPowerOffNotification as show below:

#import <AppKit/AppKit.h>

@interface ShutDownHandler : NSObject <NSApplicationDelegate>

- (void)computerWillShutDownNotification:(NSNotification *)notification;

@end


int main(int argc, char* argv[]) {
    NSNotificationCenter *notCenter;

    notCenter = [[NSWorkspace sharedWorkspace] notificationCenter];

    ShutDownHandler* sdh = [ShutDownHandler new];

    [NSApplication sharedApplication].delegate = sdh;

    [notCenter addObserver:sdh
                  selector:@selector(computerWillShutDownNotification:)
                      name:NSWorkspaceWillPowerOffNotification
                    object:nil];

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [[NSFileManager defaultManager] createFileAtPath:@"./output.txt" contents:nil attributes:nil];
    });

    [[NSRunLoop currentRunLoop] run];

    return 0;
}


@implementation ShutDownHandler

- (void)computerWillShutDownNotification:(NSNotification *)notification {
    NSFileHandle* file = [NSFileHandle fileHandleForUpdatingAtPath: @"./output.txt"];
    [file seekToEndOfFile];

    NSDateFormatter* fmt = [NSDateFormatter new];
    [fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate* current = [NSDate date];

    NSString* dateStr = [fmt stringFromDate:current];
    [dateStr writeToFile:@"./output.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
}

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
    return NSTerminateCancel;
}

@end

For some reason that I cannot understand, the NSWorkspaceWillPowerOffNotification handler is never called!

Also added the following to my .plist:

<key>NSSupportsSuddenTermination</key>
<false/>

but still no notification is register when shutting down my system.

Any idea as to why??


回答1:


For future reference:

I was not able to register to the above event and see it fire.

Finally, I went with a different approach: apple open-source power management

Here, you can see we have notifications names such as: "com.apple.system.loginwindow.logoutNoReturn"

You can register to those, and that will do the trick.

Hope this will help someone one day :)




回答2:


When you say you've ran your code as a background process, do you mean that it's based on launchd daemon handled according to plist file in /Library/LaunchDeamon ? In this case, the notification will not be sent. Try running it under Cocoa application

Seems like this notification doesn't work on LaunchDaemon linked against AppKit.

I'm still looking for a way to get these notifications on background deamon process.



来源:https://stackoverflow.com/questions/46085718/nsworkspacewillpoweroffnotification-never-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!