[英]How to get the right date from UIDatePicker for Local Notification
- (IBAction)addReminder:(id)sender {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *date = [self.datePicker date];
//break date up:
NSDateComponents *dateComps = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
NSDateComponents *timeComps = [calendar components:NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond fromDate:date];
//set the fire time:
NSDateComponents *notificationDayComps = [[NSDateComponents alloc] init];
[notificationDayComps setDay:[dateComps day]];
[notificationDayComps setMonth:[dateComps month]];
[notificationDayComps setYear:[dateComps year]];
[notificationDayComps setHour:[timeComps hour]];
[notificationDayComps setMinute:[timeComps minute]];
[notificationDayComps setSecond:[timeComps second]];
NSDate *notificationDate = [calendar dateFromComponents:notificationDayComps];
NSLog(@"Setting a reminder for %@", notificationDate);
UILocalNotification *note = [[UILocalNotification alloc] init];
if (note == nil) return;
note.alertBody = @"Hypnotize me!";
note.fireDate = notificationDate;
note.timeZone = [NSTimeZone defaultTimeZone];
//schedule the notification:
[[UIApplication sharedApplication] scheduleLocalNotification:note];
}
这是我的代码。 我正在阅读《大书呆子牧场》一书。 最初,他们告诉我这样做:
- (IBAction)addReminder:(id)sender {
NSDate *date = self.datePicker.date;
NSLog(@"Setting a reminder for %@", date);
}
当我点击在两种方法上都触发代码的按钮时,记录的日期不是同一日期。 如果我使用当前区域设置登录,则会显示正确的日期,但是当我在iPhone上对其进行测试时,它不会使用正确的日期作为提醒。 我试图在上面添加日历组件,并尝试了来自此处和其他站点的许多其他提示,但是无法获取它来记录正确的日期或为本地通知设置正确的日期。
2015-11-09 12:42:55.415 HypnoNerd[1251:83127] Setting a reminder for 2015-11-09 20:43:00 +0000
这是当我今天在日期选择器上选择12:42时显示的内容,但显然它记录了错误的日期。 我知道NSDate与典型的日历日期不同,但是我仍然无法弄清楚如何进行此工作,因此任何见解都会很棒。 谢谢
- (IBAction)addReminder:(id)sender {
self.datePicker.timeZone = [NSTimeZone defaultTimeZone];
NSDate *date = [self.datePicker date];
UILocalNotification *note = [[UILocalNotification alloc] init];
note.alertBody = @"Hypnotize me!";
note.fireDate = date;
note.timeZone = [NSTimeZone defaultTimeZone];
//schedule the notification:
UIApplication *app = [UIApplication sharedApplication];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert) categories:nil];
[app registerUserNotificationSettings:settings];
[app scheduleLocalNotification:note];
}
这行得通,但我不太了解发生了什么。 如果有人能从更一般的意义上解释这个过程,我将不胜感激。 谢谢您的帮助。
您需要将日期选择器日期转换为localtimezone。
默认为UTC timeZone。使用以下代码获取当前日期。
NSDateFormatter *userFormatter = [[NSDateFormatter alloc] init];
[userFormatter setTimeZone:[NSTimeZone localTimeZone]];
[userFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSString *dateConverted = [userFormatter stringFromDate:self.Datepick.date];
NSLog(@"%@",dateConverted);
“这些不是您要寻找的机器人。”
我认为OP的问题在于未触发本地通知,并且错误的结论是这与时区设置有关。 内部时间以格林尼治标准时间(又称为“ UTC”)报告。 我们可以在发布的日志消息中看到这一点:
2015-11-09 20:43:00 +0000
其中+0000
表示时间未从格林尼治标准时间+
或-
移出。 无需将此时间报告为本地时间。 实际上,不需要弄乱任何.timeZone
属性。
@ crypt3c的后续文章中已解决的问题与以下调用有关:
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert) categories:nil]
[app registerUserNotificationSettings:settings];
从iOS 8开始,要求用户向应用授予权限以接收一种或多种通知类型; 未经许可,通知将失败。
因此,iOS 8及更高版本的正确代码是:
- (IBAction)addReminder:(id)sender {
NSDate *date = [self.datePicker date];
UILocalNotification *note = [[UILocalNotification alloc] init];
note.alertBody = @"Hypnotize me!";
note.fireDate = date;
//schedule the notification:
UIApplication *app = [UIApplication sharedApplication];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert) categories:nil];
[app registerUserNotificationSettings:settings];
[app scheduleLocalNotification:note];
}
定义了四个UIUserNotificationTypes
:
UIUserNotificationTypeNone
UIUserNotificationTypeBadge
UIUserNotificationTypeSound
UIUserNotificationTypeAlert
有关此主题的更多信息,请参阅 iOS开发者库中的UIUserNotificationSettings类参考 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.