I need to get a date and time from a UIDatePicker and fire a local notification when that date and time has arrived. How would I go about doing this? Thanks!
Asked
Active
Viewed 1,614 times
0
-
1possible duplicate of [Register for Local Notification](http://stackoverflow.com/questions/9529590/register-for-local-notification) - that question even pulls the date from a date picker, as does [Extract date from UIDatePicker](http://stackoverflow.com/questions/4172079/extract-date-from-uidatepicker?rq=1) – Tim Mar 12 '13 at 17:11
-
http://stackoverflow.com/questions/12277334/schedulelocalnotification-doesnt-work/12277465#12277465 – Rajneesh071 Mar 12 '13 at 17:17
2 Answers
3
use this code
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate = your date from date picker;
// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = @"Write your text here";
// Set the action button
localNotif.alertAction = @"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
// Specify custom data for the notification
NSStream *str=[NSString stringWithFormat:@"%d",cId];
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:str forKey:@"NotificationDate"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(@"The notifications are:%@",[[UIApplication sharedApplication]scheduledLocalNotifications]);
NSLog(@"The %@",localNotif.userInfo);
[localNotif release];

Pradip
- 677
- 9
- 26
-
Thanks! So this will get a date from a date picker in my view, then fire a notification when that date and time has arrived? – Evan Mar 21 '13 at 15:48
-
-
Pradip I have this code in my app, however how would I go about changing the format to display mm/dd/yyyy and a 12 hour clock? Here is a link to my code and question http://stackoverflow.com/questions/16174337/uidatepicker-displays-2013-04-23-161210-instead-of-1110 – flyers Apr 26 '13 at 15:44
1
Use self.datePicker.date
to store the target date with time.
Then make a NSTimer that will poll every miilisecond to check if current date equals to target date. As both become equal then show the alertview.

Anoop Vaidya
- 46,283
- 15
- 111
- 140
-
Thanks for answering, but could you give me a code example? I'm not familiar with this part of Objective-C. – Evan Mar 12 '13 at 17:37