我希望你能够通过我正在制作的应用程序关闭、重启、注销和休眠我的系统。我找不到任何本地的Objective C方法来完成这个任务,这真的很困难。
有人可以指导我如何最好地完成这个任务吗?
我已经尝试过:
- 使用NSTask来运行shell脚本
- 使用AppleScript
- 使用IOPMAssertionDeclareUserActivity函数来使系统进入睡眠状态
NSString *scriptAction = @"restart"; // @"restart"/@"shut down"/@"sleep"/@"log out"
NSString *scriptSource = [NSString stringWithFormat:@"tell application \"Finder\" to %@", scriptAction];
NSAppleScript *appleScript = [[[NSAppleScript alloc] initWithSource:scriptSource] autorelease];
NSDictionary *errDict = nil;
if (![appleScript executeAndReturnError:&errDict]) {
//
}
那一点也没有运气,还尝试了以下方法:
NSAppleScript* theScript = [[NSAppleScript alloc] initWithSource:
@"Tell application \"Finder\" to restart"];
if (theScript != NULL)
{
NSDictionary* errDict = NULL;
// execution of the following line ends with EXC
if (YES == [theScript compileAndReturnError: &errDict])
{
[theScript executeAndReturnError: &errDict];
}
[theScript release];
}
运气不好