2010-12-22 25 views
4

我需要能夠實現的方法:關機的Mac目標C

  • 關機
  • 重啓
  • 註銷
  • 睡眠

在Mac上,我使用的XCode但似乎無法弄清楚執行這些操作的代碼。

任何人都可以幫助我嗎?

感謝

+0

此鏈接提供C示例代碼,你想要做的一切:http://developer.apple。 com/library/mac /#qa/qa2001/qa1134.html – 2010-12-22 02:27:34

回答

10

一個不重要/偷懶的方法做,這是通過一些簡單的內聯的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); 
} 
+0

我無法讓您的代碼正常工作,但我已經在網上找到了與您的代碼相關的代碼。這個工作並且被測試。 http://pastebin.com/eWhfc5eR – 2010-12-22 12:05:33