Shutdown Mac Objective C

耗尽温柔 提交于 2019-12-01 04:24:27

问题


I need to be able to implement methods of:

  • Shutdown
  • Restart
  • Logoff
  • Sleep

On a Mac, I am using XCode but can't seem to figure out the code to perform these actions.

Can anyone help me out here?

Thanks


回答1:


A trivial/lazy way do this is via some simple inline Applescript:

NSString *scriptAction = @"restart"; // @"restart"/@"shut down"/@"sleep"/@"log out"
NSString *scriptSource = [NSString stringWithFormat:@"tell application \"Finder\" to %@", scriptAction];
NSAppleScript *appleScript = [[NSAppleScript alloc] initWithSource:scriptSource];
NSDictionary *errDict = nil;
if (![appleScript executeAndReturnError:&errDict]) {
    NSLog(@"%@", errDict); 
}


来源:https://stackoverflow.com/questions/4505632/shutdown-mac-objective-c

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