Programmatically put a Mac into sleep

后端 未结 6 1002
清歌不尽
清歌不尽 2021-02-03 13:21

I can\'t find any instructions how to put a Mac programmatically into sleep mode (in Objective-C). I\'m sure it should be only one line, but could you give me a hint?

6条回答
  •  不知归路
    2021-02-03 13:53

    Tom is correct. The AE methods fail if the display is sleeping. pmset sleepnow works 100%.

    NSTask  *pmsetTask = [[NSTask alloc] init];
    pmsetTask.launchPath = @"/usr/bin/pmset";
    pmsetTask.arguments = @[@"sleepnow"];
    [pmsetTask launch];
    

提交回复
热议问题