2013-04-05 75 views
1

我在mac上編寫objective-c中的應用程序,我需要一些幫助。每一天,在23點59分,我的應用程序調用一個方法來生成NSTimer,NSRunloop和深度睡眠的mac

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

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

一份報告,工作正常,除非計算機處於深度睡眠模式。事情是:我需要計時器在這一天的某個時刻調用該方法,即使它處於睡眠模式。我不想阻止睡眠。你可以幫我嗎?

在此先感謝!

回答

3

退房IOPMSchedulePowerEvent:

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

注意,這必須從根部被調用。

+0

感謝

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]; 

...我我會看看這個,看看它是否有效! – 2013-04-05 20:22:41