2012-02-12 13 views

答えて

7

はちょうどそれを見つけた:

- (void) receiveWakeNote: (NSNotification*) note 
{ 
    NSLog(@"receiveSleepNote: %@", [note name]); 
} 

- (void) fileNotifications 
{ 
    //These notifications are filed on NSWorkspace's notification center, not the default 
    // notification center. You will not receive sleep/wake notifications if you file 
    //with the default notification center. 
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self 
                  selector: @selector(receiveWakeNote:) 
                   name: NSWorkspaceDidWakeNotification object: NULL]; 
} 
+0

2つのこと:あなたがオブジェクトのない "NULL" "nilを" したい、とあなたはフォーマットする必要がありますあなたの答えをフォーマットされたコードとして表示する - それは今はかなり読めません。しかし、あなた自身の質問に答える良い仕事! –

+0

コードを適切にフォーマットするにはどうすればよいですか?私は必要なタグを知りません...ありがとう! –

+0

Paragがあなたにそれを打つように見える。しかし、将来はエディタのボタンを見てください。それらの1つは中括弧( "{}")です。これを使用して、選択したテキストブロックをコードとしてフォーマットします。 –

2

あなたはIORegisterForSystemPower()を使用することができます。

は、システムのために睡眠&ウェイク通知を受信するため のルート電源ドメインIOServiceのに発信者を接続します。 は、システムシャットダウンと再起動通知を提供しません。

io_connect_t IORegisterForSystemPower (
    void *refcon, 
    IONotificationPortRef *thePortRef, 
    IOServiceInterestCallback callback, 
    io_object_t *notifier) ; 

迅速な3についてQ:How can my application get notified when the computer is going to sleep or waking from sleep? How to I prevent sleep?

2

を見てみましょう:

func onWakeNote(note: NSNotification) { 
    print("Received wake note: \(note.name)") 
} 

func onSleepNote(note: NSNotification) { 
    print("Received sleep note: \(note.name)") 
} 

func fileNotifications() { 
    NSWorkspace.shared().notificationCenter.addObserver(
     self, selector: #selector(onWakeNote(note:)), 
     name: Notification.Name.NSWorkspaceDidWake, object: nil) 

    NSWorkspace.shared().notificationCenter.addObserver(
     self, selector: #selector(onSleepNote(note:)), 
     name: Notification.Name.NSWorkspaceWillSleep, object: nil) 
} 
関連する問題