1

我正在mac上用objective-c编写一个应用程序,我需要一些帮助。每天,在 23:59,我的应用程序正在调用一个方法来生成报告

NSTimer *timer = [[NSTimer alloc]
                  initWithFireDate:endOfTheDay
                  interval:0.0
                  target:self
                  selector:@selector(endDay)
                  userInfo:nil
                  repeats:NO];

[[NSRunLoop currentRunLoop] addTimer:timer forMode: NSDefaultRunLoopMode];

除非计算机处于深度睡眠模式,否则它会按预期工作。问题是:即使它处于睡眠模式,我也需要计时器在一天中的这个确切时刻调用该方法。我不想阻止睡眠。你能帮助我吗?

提前致谢!

4

2 回答 2

3

查看 IOPMSchedulePowerEvent:

 IOReturn IOPMSchedulePowerEvent(
   CFDateRef time_to_wake,
   CFStringRef my_id,
   CFStringRef type);

请注意,这必须从 root 调用。

于 2013-04-05T20:03:10.907 回答
0

我实际上所做的是使用我的参数创建一个 pmset 事件,并使用 AppleScript 行以 root 身份运行它,如下所示:

NSDate *currentDate = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *currentComponents = [cal components: NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:currentDate];
NSDate *lastDay = [cal dateFromComponents:currentComponents];
NSDateComponents *aDay = [[NSDateComponents alloc] init];
aDay.day = 1;
NSDate *tonight = [cal dateByAddingComponents:aDay toDate:lastMidnight options:0];

NSString *command = [NSString stringWithFormat: @"pmset schedule wake %@, tonight]
NSString *scriptLine= @"[NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges, command]"

NSAppleScript *myScript = [[NSAppleScript new] initWithSource:scriptLine];
NSAppleEventDescriptor *res = [myScript executeAndReturnError:&errorInfo];

基于 AuthorizationExecuteWithPrivileges 已被弃用并且

于 2013-04-15T17:51:41.473 回答