简体   繁体   English

如何从UIDatePicker获取正确的日期以进行本地通知

[英]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];
}

Here is my code. 这是我的代码。 I am reading from the Big Nerd Ranch book. 我正在阅读《大书呆子牧场》一书。 Originally they told me to do this: 最初,他们告诉我这样做:

- (IBAction)addReminder:(id)sender {
    NSDate *date = self.datePicker.date;
    NSLog(@"Setting a reminder for %@", date);
}

When I hit the button that triggers the code on both methods the date that is logged is not the same date. 当我点击在两种方法上都触发代码的按钮时,记录的日期不是同一日期。 If I log it with current locale it prints the right date, but when I test it on an iphone it does not use the right date for the reminder. 如果我使用当前区域设置登录,则会显示正确的日期,但是当我在iPhone上对其进行测试时,它不会使用正确的日期作为提醒。 I tried to add the calendar components above and tried many other tips from here and other sites but cannot get it to log the right date or set the right date for a local notification. 我试图在上面添加日历组件,并尝试了来自此处和其他站点的许多其他提示,但是无法获取它来记录正确的日期或为本地通知设置正确的日期。

2015-11-09 12:42:55.415 HypnoNerd[1251:83127] Setting a reminder for 2015-11-09 20:43:00 +0000

This is what shows up when I choose 12:42 today on the date picker, but clearly it logs the wrong date. 这是当我今天在日期选择器上选择12:42时显示的内容,但显然它记录了错误的日期。 I know NSDate is not the same as a typical calendar date, but I still cannot figure out how to make this work so any insight would be wonderful. 我知道NSDate与典型的日历日期不同,但是我仍然无法弄清楚如何进行此工作,因此任何见解都会很棒。 Thanks 谢谢

- (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];
}

This works, but I do not really understand what is going on. 这行得通,但我不太了解发生了什么。 If anyone could explain this process in a more general sense I would appreciate it. 如果有人能从更一般的意义上解释这个过程,我将不胜感激。 Thanks for the help. 谢谢您的帮助。

You need to convert your datepicker date into localtimezone. 您需要将日期选择器日期转换为localtimezone。

Default it has UTC timeZone.Use Below code to get current Date. 默认为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);

"These aren't the droids you are looking for." “这些不是您要寻找的机器人。”

I believe the OP's issue was the fact that local notifications were not being triggered and the incorrect conclusion was that this had something to do with time zone settings. 我认为OP的问题在于未触发本地通知,并且错误的结论是这与时区设置有关。 The internal time is reported back in GMT (aka "UTC"). 内部时间以格林尼治标准时间(又称为“ UTC”)报告。 We can see this in the posted log message: 我们可以在发布的日志消息中看到这一点:

 2015-11-09 20:43:00 +0000

Where the +0000 is the indicator that the time has not been shifted either + or - from GMT. 其中+0000表示时间未从格林尼治标准时间+-移出。 There is no need for this time to be reported as local time. 无需将此时间报告为本地时间。 In fact, there is no need to mess around with any of the .timeZone properties. 实际上,不需要弄乱任何.timeZone属性。

The problem, which is solved in the followup post from @crypt3c is related to the following calls: @ crypt3c的后续文章中已解决的问题与以下调用有关:

 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert) categories:nil]
 [app registerUserNotificationSettings:settings];

As of iOS 8, it is required that a user grants permission to an app to receive one or more notification types; 从iOS 8开始,要求用户向应用授予权限以接收一种或多种通知类型; without having granted permission, notifications will fail. 未经许可,通知将失败。

Therefore the correct code for iOS 8 and higher is: 因此,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];
 }

There are four UIUserNotificationTypes defined: 定义了四个UIUserNotificationTypes

UIUserNotificationTypeNone
UIUserNotificationTypeBadge
UIUserNotificationTypeSound
UIUserNotificationTypeAlert

More information on this topic is available in the UIUserNotificationSettings Class Reference from the iOS Developer Library. 有关此主题的更多信息,请参阅 iOS开发者库中的UIUserNotificationSettings类参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM