[英]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.