How to get Restart,Shutdown .... events

Discussion in 'Mac Programming' started by Sunil, Jul 13, 2007.

  1. macrumors newbie

    Hi Guys,

    I have written a network application in cocoa.
    In my application I want to capture the Logout,Restart,Shutdown events.
    Based on these events I want to send some message to my server.

    After reading the documentation on internet regarding system events I came to know that whenever user choose "Shutdown" or "Restart" from the Apple Menu the running application gets "KAEQuitApplication" event but not any specific event whether the application is quiting by "Shutdown" or "Restart" or "Log Out". The "KAEQuitApplication" event also get by the user when he/she quit their application by clicking at the top left cross button of the application window.

    I m looking to get the specific event related with each action taken by the user i.e. "Shutdown" or "Restart" or "Log Out".
    Is it possible to get the different event for each of the action taken by the user i.e. "Shutdown" or "Restart" or "Log Out"?.

    waiting for the reply.

  2. macrumors 603


    I've no idea. But you could try seeing if a notification is sent, by registering for all distributed notifications; restarting your Mac, and seeing what notifications were received.
  3. macrumors 68030


    #3 <-- NotificationWatcher could work as well.
  4. Moderator emeritus


    I think these may be what you're looking for: kIOMessageSystemWillPowerOff, kIOMessageSystemWillSleep, kIOMessageSystemWillRestart
  5. macrumors 6502a

    Heres some code from my Utility Lock, i believe you could just change the constants in the case;

    #import "PowerManagment.h"
    void powerCallback(void *refCon, io_service_t service, natural_t messageType, void *messageArgument)
        [(PowerManagment *)refCon powerMessageReceived: messageType withArgument: messageArgument];
    @implementation PowerManagment
    - (void)powerMessageReceived:(natural_t)messageType withArgument:(void *) messageArgument
        switch (messageType)
            case kIOMessageSystemWillSleep:{
                IOAllowPowerChange(root_port, (long)messageArgument);
                NSLog(@"System is going to sleep");
            case kIOMessageCanSystemSleep:
                // I don’t know if this will ever be asked…
                IOAllowPowerChange(root_port, (long)messageArgument);
            case kIOMessageSystemHasPoweredOn:{
                NSLog(@"System has awaken from sleep");
    - (void)awakeFromNib {
        IONotificationPortRef notificationPort;
        root_port = IORegisterForSystemPower(self, &notificationPort, powerCallback, &notifier);
        NSAssert(root_port != nil, @"IORegisterForSystemPower failed");
        CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notificationPort), kCFRunLoopDefaultMode);
    #import <Cocoa/Cocoa.h>
    #import <IOKit/pwr_mgt/IOPMLib.h>
    #import <IOKit/IOMessage.h>
    @interface PowerManagment : NSObject {
    	io_connect_t root_port;
    	io_object_t notifier;
    - (void)powerMessageReceived:(natural_t)messageType withArgument:(void *) messageArgument;

Share This Page