Reputation: 59
I'm programming an app in objective-c on mac and I need some help. Each day, at 23:59, my app is calling a method to generate a report with
NSTimer *timer = [[NSTimer alloc]
initWithFireDate:endOfTheDay
interval:0.0
target:self
selector:@selector(endDay)
userInfo:nil
repeats:NO];
[[NSRunLoop currentRunLoop] addTimer:timer forMode: NSDefaultRunLoopMode];
which is working as expected unless the computer is in deep sleep mode. The thing is : I need the timer to call the method at this exact moment of the day even if it is in sleep mode. I'd like not to have to prevent sleep. Can you help me?
Thanks in advance!
Upvotes: 1
Views: 543
Reputation: 59
What I actually did, is create a pmset event with my arguments and running it as root with an AppleScript line something like this :
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];
Based on AuthorizationExecuteWithPrivileges is deprecated and
Upvotes: 0
Reputation: 5817
Check out IOPMSchedulePowerEvent:
IOReturn IOPMSchedulePowerEvent(
CFDateRef time_to_wake,
CFStringRef my_id,
CFStringRef type);
Note that this must be called from root.
Upvotes: 3