我正在开发一款应用,该应用会向用户发送本地通知,提醒他们某双鞋即将上市。
我认为应该在前一天晚上 8:00 发送通知。
这是我的日期字符串(称为 releaseDate):
2014-04-10 00:00:00 +0000
如何在鞋子发布前一天晚上 8:00 向用户发送本地通知?
- (IBAction)addReminder:(id)sender {
// Get the current date
NSDate *reminderDate = ??;
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = reminderDate;
localNotification.alertBody = @"Hi";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
谢谢。
最佳答案
例如,将您的日期减少到一天
[[NSDate date] dateByAddingTimeInterval:60*60*24*-1];
编辑:另一种方式
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDateComponents * comps = [NSDateComponents new];
[comps setDay:-1];
NSDate * yesterdayDate = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
NSLog(@"%@", yesterdayDate);
终于解决了如何获得昨天晚上 8 点的日期
NSDate * releaseDate = [NSDate date];
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDateComponents * comps = [NSDateComponents new];
[comps setDay:-1];
NSDate * yesterdayDate = [calendar dateByAddingComponents:comps toDate:releaseDate options:0];
NSDateComponents * comps2 = [calendar components:NSCalendarUnitDay|NSCalendarUnitMonth| NSCalendarUnitYear fromDate: yesterdayDate];
[comps2 setHour:20];
[comps2 setMinute:0];
NSDate * yesterday8PM = [calendar dateFromComponents:comps2];
NSLog(@"%@", yesterday8PM);
关于ios - 在某个日期之前设置 localNotification 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22917809/