我正在尝试为我的本地通知设置一个有条件的触发时间,但是当我尝试这两种方法时,它并没有失败,但它仍然没有用。所以,我想知道我是否能够做这样的事情?
注意: startTime 和 endTime 是来自日期选择器的时间。
-(void) TEST {
NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
NSDateComponents *components = [calendar components:NSHourCalendarUnit fromDate:[NSDate date]];
components.hour = 3;
components.minute = (components.minute + 1) % 60;
components.second = 57;
NSDate *fire = [calendar dateFromComponents:components];
UILocalNotification *notif = [[UILocalNotification alloc] init] ;
if (notif == nil)
return;
if (fire < startTime.date) {
notif.fireDate =fire ;
notif.repeatInterval= NSMinuteCalendarUnit ;
notif.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
[[UIApplication sharedApplication] scheduleLocalNotification:notif] ;
}
if (fire > endTime.date) {
notif.fireDate =fire ;
notif.repeatInterval= NSMinuteCalendarUnit ;
notif.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
[[UIApplication sharedApplication] scheduleLocalNotification:notif] ;
}}
或者
-(void) TEST {
NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
NSDateComponents *components = [calendar components:NSHourCalendarUnit fromDate:[NSDate date]];
components.hour = 3;
components.minute = (components.minute + 1) % 60;
components.second = 57;
NSDate *fire = [calendar dateFromComponents:components];
UILocalNotification *notif = [[UILocalNotification alloc] init] ;
if (notif == nil)
return;
if (fire > startTime.date & fire < endTime.date) {
[[UIApplication sharedApplication] cancelAllLocalNotifications] ;
}
else {
notif.fireDate =fire ;
notif.repeatInterval= NSMinuteCalendarUnit ;
notif.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
[[UIApplication sharedApplication] scheduleLocalNotification:notif] ;
}}
谢谢
如果不是,那么产生这种条件的最简单方法是什么?