set datepicker at a specific time - iphone

Hi guys i have a problem
i my code i set a datepicker with the date of today, after that if the user change the picker i set a alarm to ring at the date of picker.
How can i "block the picker" on these date?
here my code, i hope in your help
NSDate *pickerDate = [NSDate date];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Divisione della data in fattori
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set Up dell'alarme considerando tutti i fattori
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]];
//Notifica in meno di un minuto
[dateComps setSecond:0];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
[alarmDatePickerUno setDate:itemDate animated:YES];
[alarmDatePickerDue setDate:itemDate animated:YES];
}
// ALARM UNO
// Set Allarme Uno
-(IBAction)setAlarmUno:(id)sender{
NSLog(#"picker in allarme");
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
//Setting dell'allarme in base al picker
AlarmDateUno = [alarmDatePickerUno date];
NSLog(#"allarme settata alle");
NSLog(#"%#", AlarmDateUno);

alarmDatePickerUno.userInteractionEnabled = NO;
alarmDatePickerDue.userInteractionEnabled = NO;
EDIT AFTER CLARIFICATION
If you want to use a date between app launches, you need to persist the date somehow. I would suggest using NSUserDefaults, but you could also go for the more complicated solution of Core Data. Using NSUserDefaults you would check every time you create your UIDatePicker for the saved value and use that to initialize the picker.
// Get the date
NSDate *unoDate = [[NSUserDefaults standardUserDefaults] objectForKey:#"PickerUnoKey"];
if (unoDate) { // If we have a persisted date, we set the date of the datePicker
[alarmDatePickerUno setDate:unoDate animated:NO];
}
Then when the user changes the value of the picker, you need to store the new value.
// When the datePicker value has changed, we store the value
[[NSUserDefaults standardUserDefaults] setObject:alarmDatePickerUno.date forKey:#"PickerUnoKey"];

Related

sethours in NSDateComponents

I'm trying to sethours in NSDateComponents, I wrote the following code
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:intDay];
[dateComps setMonth:intMonth];
[dateComps setYear:intYear];
[dateComps setHour:intHoures];
[dateComps setMinute:intMinutes];
NSDate *itemDate;
itemDate = [calendar dateFromComponents:dateComps];
NSLog(#"reminder date: %#", itemDate);
but when I set the hours as 13, it sets it as 11. I want hours in 24 style. Anyone can help me in this issue?
Thanks in Advance.
Time difference in the hours indicate that you might have problem with time zone, you could try to set the timezone for the date, I know NSDateFormatter has timeZone Property,I doubt if NSDateComponents have timeZone property. However you could set the timeZone for calendar
[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];

Only let local notification appear once

I have a huge problem! My problem is, that I've implementet a localnotification code in my delegate. I've put in this code in didEnterBackround method. However, I get a localnotifi for each didEnterBackround. Is there a possible way that only a new notification firedate starts, if the latest was gone? Like if I have a localnotifi which fires after 5 days. When I open and close the app 5 times and wait 5 days I get 5 localnotifications. Is it possible to let the app make only one firedate and after this appears a new one gets startet?!
My code:
NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *dateComponents = [calender components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:currentDate];
NSDateComponents *timeComponents = [calender components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:currentDate];
NSDateComponents *temp = [[NSDateComponents alloc]init];
[temp setYear:[dateComponents year]];
[temp setMonth:[dateComponents month]];
[temp setDay:[dateComponents day]];
[temp setHour:[timeComponents hour]];
[temp setMinute:[timeComponents minute]];
[temp setSecond:[timeComponents second] +10];
NSDate *fireTime = [calender dateFromComponents:temp];
UILocalNotification *localN = [[UILocalNotification alloc]init];
localN.fireDate = fireTime;
localN.timeZone = [NSTimeZone defaultTimeZone];
localN.alertBody = #"BLA BLA BLA!";
localN.alertAction = #"GO BACK";
localN.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localN];
You're able to cancel all scheduled notifications:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
If you don't want to remove ALL scheduled notifications (perhaps you have something else scheduled), then take a look at cancelLocalNotification: which allows you to cancel only a single instance.

LocalNotifiction for wakeup application

In my application i want to set local notifiaction on a particular time if my application killed from background else if my application in running condition then i dan't want to show localnotification
I have written code for local notification
NSCalendar *calendar1 = [NSCalendar autoupdatingCurrentCalendar];
// Get the current date
NSDate *pickerDate1 =self.dateEvening;//[self.dateEvening dateByAddingTimeInterval:60*1];// [self.pickerView date];
// Break the date up into components
NSDateComponents *dateComponents1 = [calendar1 components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate1];
NSDateComponents *timeComponents1 = [calendar1 components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate1];
NSDateComponents *dateComps1 = [[NSDateComponents alloc] init];
[dateComps1 setDay:[dateComponents1 day]];
[dateComps1 setMonth:[dateComponents1 month]];
[dateComps1 setYear:[dateComponents1 year]];
[dateComps1 setHour:[timeComponents1 hour]];
[dateComps1 setMinute:[timeComponents1 minute]];
[dateComps1 setSecond:[timeComponents1 second]];
NSDate *itemDate1 = [calendar1 dateFromComponents:dateComps1];
[dateComps1 release];
UILocalNotification *localNotif1 = [[UILocalNotification alloc] init];
if (localNotif1 == nil)
return;
localNotif1.fireDate = itemDate1;
localNotif1.timeZone = [NSTimeZone defaultTimeZone];
localNotif1.alertBody = #"Open application";
localNotif1.alertAction = #"View";
localNotif1.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif1];
[localNotif1 release];
Above code works fine if application will close from background but if my application in running condition then I don't want local notifications to be fired.

How i campare two dates in Iphone?

I want to set date and time two days after my current date and time.How i set it using NSDate and dateformatter. I want compare date after two days, it was perviously set or not.
I write code now for it,
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *pickerDate = [self.datePicker date];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
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:00];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
appDelegate.setTimerByUser = [itemDate retain];
How i compare it with current time and date?
Thanks
Look in NSDate.h and read Apple's docs for the -compare function on NSDates.
You can do for example:
NSDate *now = [NSDate date];
NSDate *future = [now dateByAddingTimeInterval: 2 * 24 * 60 * 60]; // add two days
if ( [now compare: future] == NSOrderedAscending ) {
// now is earlier than future so we will always get here
} else {
// never reached but illustrates how you might use compare
}
I think you need NSDateComponents. You can use this class to get year, month, day, and other attributes about date and time.
NSDate * now = [NSDate date];
NSDate * twoDaysFromNow = [now dateByAddingTimeInterval:(2 * 24* 60 * 60)];
You can compare with NSDate's -earlierDate: or -laterDate: methods.

If/Else statement for Date..iPhone SDK

I have this bit of code here. How would I write an if/else statement to determine whether the itemDate has already passed? So if itemDate has already passed, then localnotif.fireDate should be what it is now, plus 86400 (for 24 hours) else leave as is.
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:item.day];
[dateComps setMonth:item.month];
[dateComps setYear:item.year];
[dateComps setHour:item.hour];
[dateComps setMinute:item.minute];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [itemDate dateByAddingTimeInterval:-(minutesBefore*60)] ;
NSLog(#"fireDate is %#",localNotif.fireDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];
You can retrieve the current date using [NSDate date].
NSDate has this really convenient method called laterDate: that compares a date with the receiver and returns which ever one is later.
You can see if two dates are equal using isEqual:
In other words:
NSDate * notificationDate = [localNotif fireDate];
NSDate * now = [NSDate date];
if ([[now laterDate:notificationDate] isEqual:now]) {
//in a comparison between "now" and "notificationDate", "now" is the later date,
//meaning "notificationDate" has already passed
notificationDate = [notificationDate dateByAddingTimeInterval:86400];
}
[localNotif setFireDate:notificationDate];