我想得到每日通知,为此我用google搜索 我从这个iPhone : Daily local notifications得到了一些解决方案,但我无法正确识别
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay: 3];
[components setMonth: 7];
[components setYear: 2012];
[components setHour: 6];
[components setMinute: 0];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone defaultTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
假设当前时间 3:13,当前日期为20-11-2014 在这里我想在 3:14,20-11-2014 时设置本地通知。任何人都可以帮助我,因为我尝试了以下事情但没有工作
[components setDay: 20];
[components setMonth: 11];
[components setYear: 2014];
[components setHour: 15];
[components setMinute: 14];
[components setSecond: 0];
答案 0 :(得分:1)
NSDate *date = [NSDate date];
NSDate *newDate1 = [date dateByAddingTimeInterval:60*60*24];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = newDate1;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [[NSUserDefaults standardUserDefaults] objectForKey:@"NotificationText"];
localNotif.alertAction = @"View";
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
试试这个希望这对你有所帮助。谢谢
答案 1 :(得分:-1)
NSDate* now = [NSDate date] ;
NSDateComponents* tomorrowComponents = [NSDateComponents new] ;
tomorrowComponents.day = 1 ;
NSCalendar* calendar = [NSCalendar currentCalendar] ;
NSDate* tomorrow = [calendar dateByAddingComponents:tomorrowComponents toDate:now options:0] ;
NSDateComponents* tomorrowAt7AMComponents = [calendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:tomorrow] ;
tomorrowAt7AMComponents.hour = 7 ;
NSDate* tomorrowAt7AM = [calendar dateFromComponents:tomorrowAt7AMComponents] ;
localnotification.fireDate = tomorrowAt7AM;
我希望这会对你有所帮助。