我编写了这段代码,以便通过睡眠时间对我的通知执行暂停,但是缺少一些我无法弄清楚的东西。会是什么呢?
我的.m
代码:
- (IBAction)save:(id)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter.timeStyle=NSDateFormatterShortStyle;
dateFormatter.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString=[dateFormatter stringFromDate:_startTime.date];
NSLog(@"Start time is %@",dateTimeString);
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
dateFormatter2.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter2.timeStyle=NSDateFormatterShortStyle;
dateFormatter2.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString2=[dateFormatter2 stringFromDate:_endTime.date];
NSLog(@"End time is %@",dateTimeString2);
if ([[NSDate date] isEqualToDate:_startTime.date]) {
NSLog(@"currentDate is equal to startTime");
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSLog(@"Sleep time silent started and notification cancelled");
}
if ([[NSDate date] isEqualToDate:_endTime.date]) {
NSLog(@"currentDate is equal to endTime");
[self pickerSelectedRow];
NSLog(@"Time to wake up and notification rescheduled");
}
}
当我点击save
按钮时,我收到这个输出,这意味着它工作正常,但是,当时间到来时,我没有得到任何输出说通知已被取消或重新安排,这意味着它不工作!!!
输出:
2013-05-03 03:04:57.481 xxx[10846:c07] Start time is 5/3/13, 3:06 AM
2013-05-03 03:04:57.482 xxx[10846:c07] End time is 5/3/13, 3:07 AM
我错过了什么?
另外,这会在后台工作吗?