how to check each and every object of array in the userdefaults - iphone

i am saving selected date from picker in array in userdefaults. So when i select a particular date and click on save that particular date is getting saved in userdefaults.This is done properly.But now i have problem.I need to compare each and every date and time of userdefaults with the current time .If an object gets equivalent to the current date and time then the notification should be shown.How is this possible.Thanks. This is my code
Here i am selecting my date through date picker and saving date in userdefaults.
this is my timepickercontroller.m
- (void)viewDidLoad {
time = [[NSString alloc] init];
CGRect pickerFrame = CGRectMake(0,40,0,0);
datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
datePicker.datePickerMode = UIDatePickerModeTime;
datePicker.date = [NSDate date];
[datePicker addTarget:self action:#selector(convertDueDateFormat) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:datePicker];
[datePicker release];
}
-(void)convertDueDateFormat{
app = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
app.timerdate = [self.datePicker date];
NSLog(#"picker date:%#",selecteddate);
if ([[NSUserDefaults standardUserDefaults] valueForKey:#"time"]==nil)
{
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:#"time"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"time"]);
}
else
{
NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:#"time"];
NSMutableArray *array_dates = [[NSMutableArray alloc] init];
for(int i =0;i<[array count];i++)
{
[array_dates addObject:[array objectAtIndex:i]];
}
[array_dates addObject:app.timerdate];
[[NSUserDefaults standardUserDefaults] setObject:array_dates forKey:#"time"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"time"]);
}
}
this is the controller where i am setting notification
-(IBAction)save{
dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"hh:mm a"];
NSDate *currentime = [NSDate date];
currentcheck = [NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:currentime] ];
NSLog(#"currenttime:%#",currentcheck);
timepicker = [[TTimePickerController alloc]init];
if (app.timerdate == NULL && interval == NULL && newsound == NULL)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"UIAlertView" message:#"Set Date,interval,sound for alarm" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert = nil;
}
else
{
[self scheduleNotification];
[self saveInDatabase];
}
}
-(void)scheduleNotification
{
[[UIApplication sharedApplication]cancelAllLocalNotifications];
timepicker = [[TTimePickerController alloc]init];
NSDate *newtestdate = [[NSUserDefaults standardUserDefaults]objectForKey:#"time"];
NSLog(#"fireDate=%#", newtestdate);
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:newtestdate];
NSDateComponents *timeComponents = [calendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:newtestdate];
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]];
itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls!= nil) {
UILocalNotification *notif = [[cls alloc]init];
notif.fireDate = itemDate;
[app.dateFormatter setDateFormat:#"hh:mm a"];
newstring = [app.dateFormatter stringFromDate:notif.fireDate];
NSLog(#"new fire date:%#",newstring);
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Alarm";
notif.alertAction = #"View";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber=1;
notif.repeatInterval = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:notif];
[notif release];
}
}

Supposing ur time format is like this hh:mm:ss
NSDate *currentTime=[NSDate date];
NSArray *timeArray=[[NSUserDefaults standardUserDefaults]objectForKey:#"time"];
NSDateFormatter* tempFormat = [[[NSDateFormatter alloc]init]autorelease];
[tempFormat setDateFormat:#"h:mm a"];
NSString* tempStr = [NSString stringWithFormat:#"%#",[tempFormat stringFromDate:date]];
if([tempStr isEqualToString:[NSString stringWithFormat:#"%#",[timeArray objectAtIndex:0]]]){
//Make any decision
//Similarly compare all the time in the array by changing the index using for-loop.
}

I suspect that what you really want to do is not to compare the dates, but to set the fire date of the notification to the date you got from the array.
To do that, you enumerate the array and launch an NSNotification for each timerDate:
NSArray *timerDates = [NSUser Defaults standardUserDefaults] objectforKey:#"time"];
NSDate *now = [NSDate date];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
// .... general setup of all notifs here.
for (NSDate * aDate in timerDates) {
localNotif.fireDate = aDate;
//... further setup of each individual notif here....
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
[localNotif release];
However, from your code, it looks like you're getting a single date from defaults:
NSDate *newtestdate = [[NSUserDefaults standardUserDefaults]objectForKey:#"time"];
but you save an array there in the method
-(void)convertDueDateFormat{
...
[[NSUserDefaults standardUserDefaults] setObject:array_dates forKey:#"time"];
So which one is it?
If you have an array, you can test its values as dates:
NSArray *timerDates = [NSUser Defaults standardUserDefaults] objectforKey:#"time"];
NSDate *now = [NSDate date];
_block int timerIndex; // can be changed inside the block
// use a block to enumerate the array
[timerDates enumerateObjectsUsingBlock:^(id obj,NSUInteger index, BOOL *stop){
if(fabs([(NSDate *)obj timeIntervalSinceDate:now])< 1.0) {
*stop=YES;
timerIndex = index;
}
} ];
Then use timerIndex to get the right date:
localNotif.firedate = [timerDates objectAtIndex:timerIndex];
Definitely also look at the documentation for comparing dates (Apple class documentation for NSDate is good). This is just one way to do it, and probably not the best.
(sorry about my formatting. I'm kinda new at this)

Related

Local Notification issue iphone

I want to make local notification that notify user 5 times a day and repeat them daily,
I have them in a mutable array which object is "hh:mm" which hours and minutes are fixed for GMT+3 town, so I get the current date and find the interval then create a date for the notification
that's the method I implement.
-first applying time zone,
-second if the time before current time so make it for the next day.
-third set local notification for that date.
plz help me
piece of code
with the use of this sample code i have schedule the 2 notification one at Morning 7AM and one at Evening 6PM and repeated it daily and it works superfine hope you can find out your solution with the use of this.
#pragma mark
#pragma mark - Notification Setup
-(void)clearNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
-(void)scheduleNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSMutableArray *arrTemp = [APPDELEGATE.userDefaults valueForKey:#"ParsingResponse"];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
NSDate *now = [NSDate date];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:7];
[components setMinute:0];
NSDate *today7am = [calendar dateFromComponents:components];
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = today7am;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.repeatCalendar = [NSCalendar currentCalendar];
notif.alertBody = [[arrTemp objectAtIndex:0] objectForKey:#"Noti_Morning"];
notif.alertAction = #"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.repeatInterval = NSDayCalendarUnit;
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:#"Morning", #"key", nil];
notif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
NSCalendar *calendar2 = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components2 = [calendar2 components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components2 setHour:18];
[components2 setMinute:0];
NSDate *today6pm = [calendar2 dateFromComponents:components2];
UILocalNotification *notif2 = [[cls alloc] init];
notif2.fireDate = today6pm;
notif2.timeZone = [NSTimeZone defaultTimeZone];
notif2.repeatCalendar = [NSCalendar currentCalendar];
notif2.alertBody = [[arrTemp objectAtIndex:0] objectForKey:#"Noti_Evening"];
notif2.alertAction = #"Show me";
notif2.soundName = UILocalNotificationDefaultSoundName;
notif2.repeatInterval = NSDayCalendarUnit;
NSDictionary *infoDict2 = [NSDictionary dictionaryWithObjectsAndKeys:#"Evening", #"key", nil];
notif2.userInfo = infoDict2;
[[UIApplication sharedApplication] scheduleLocalNotification:notif2];
[notif2 release];
}
}

Is it possible to have two local notification in app

I wanted to have two local notification ,both have different time ,let say my first notification will give alert after 1 minute and the second will give alert after 2 minutes.
I have tried it to create two in appDelegate but only first one is giving me the notification and not the second one .
How can I achieve this ?
Yes It is Possible to set two LocalNotification in any iOS Application
See below method by which you can set multiple LocalNotifications
You just Need to pass required parameter to this method.
- (void)setAlarmFor:(NSArray*)datesArray forTime:(NSString*)atTime notificationName:(NSString*)name
{
for(int dayIndex=0;dayIndex <[datesArray count];dayIndex++)
{
Class cls = NSClassFromString(#"UILocalNotification");//
if (cls != nil) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
NSString* dateStr=[datesArray objectAtIndex:dayIndex];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *tempDate = [dateFormatter dateFromString:dateStr];
NSString *tempString = [dateFormatter stringFromDate:tempDate];
tempString = [NSString stringWithFormat:#"%# %#",tempString,atTime];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSDate *firetAtThisDate = [dateFormatter dateFromString:tempString];
UILocalNotification *localNotif = [[cls alloc] init];
localNotif.fireDate =firetAtThisDate;//here set the Date at which mnotification fire;
NSLog(#"Notification date is:%#",firetAtThisDate);
localNotif.alertBody =name;
localNotif.alertAction = #"Your'Alert message";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
NSDictionary *userDict = [NSDictionary dictionaryWithObject:tempString
forKey:tempString];//by using this we can further cancel the Notification
localNotif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
[dateFormatter release];
}
}
}
And In Appdelegate Class Prepare Action what you want as Notification Fire
//This Below Line will goes to the Appdelegate DidFinishLaunching Method
Class cls = NSClassFromString(#"UILocalNotification");
if (cls)
{
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
//do what you want
}
}
application.applicationIconBadgeNumber = 0;
//End of Appdelegate DidFinishLaunching Method.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
application.applicationIconBadgeNumber = 0;
//do what you want
}
sure , you use one or more local notifications in your app. try this code in your project
-(void) setLocalNotification
{
NSTimeInterval todayTimeIntervel=[[NSDate date]timeIntervalSince1970];
NSTimeInterval nextOneMinTimeIntervel;
nextOneMinTimeIntervel = todayTimeIntervel + 60 ;
NSTimeInterval nexttwoMinTimeIntervel;
nexttwoMinTimeIntervel = todayTimeIntervel + 60*3;
NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:nextOneMinTimeIntervel];
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:nexttwoMinTimeIntervel];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSString *strDate1 = [dateFormat stringFromDate:date1];
NSString *strDate2 = [dateFormat stringFromDate:date2];
NSArray *arr = [NSArray arrayWithObjects:strDate1,strDate2, nil];
NSArray *titleArr = [NSArray arrayWithObjects:#"First LocalNotification",#"Second LocalNotification", nil];
for (int i =0; i < 2; i++)
{
NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithObjectsAndKeys:[arr objectAtIndex:i],#"dateStr",[titleArr objectAtIndex:i],#"title", nil];
[self scheduleLocalNotification:dic];
}
}
-(void) scheduleLocalNotification:(NSMutableDictionary*) dic
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm a"];
NSLog(#"%#", [dateFormat dateFromString:[dic objectForKey:#"dateStr"]]);
localNotif.fireDate = [dateFormat dateFromString:[dic objectForKey:#"dateStr"]];
localNotif.timeZone = [NSTimeZone systemTimeZone];
localNotif.alertAction = #"View";
localNotif.alertBody = [dic objectForKey:#"title"];
localNotif.userInfo = dic;
NSLog(#"value of infoDic %#",dic);
localNotif.repeatInterval = NSDayCalendarUnit;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
// get app instance
UIApplication *app = [UIApplication sharedApplication];
// create local notif
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
if (notification) {
NSDate *oneMinuteFromNow = [[NSDate date] dateByAddingTimeInterval:60];
notification.fireDate = oneMinuteFromNow;
notification.timeZone = [NSTimeZone defaultTimeZone];
NSString *notificationMessage = #"First";
notification.alertBody = notificationMessage;
notification.soundName = UILocalNotificationDefaultSoundName;
// schedule notification
[app scheduleLocalNotification:notification];
// fire notification right away
[app presentLocalNotificationNow:notification];
}
UILocalNotification *notification1 = [[[UILocalNotification alloc] init] autorelease];
if (notification1) {
NSDate *oneMinuteFromNow1 = [[NSDate date] dateByAddingTimeInterval:120];
notification1.fireDate = oneMinuteFromNow1;
notification1.timeZone = [NSTimeZone defaultTimeZone];
NSString *notificationMessage1 = #"Second";
notification1.alertBody = notificationMessage1;
notification1.soundName = UILocalNotificationDefaultSoundName;
// schedule notification
[app scheduleLocalNotification:notification1];
// fire notification right away
[app presentLocalNotificationNow:notification1];
}
By writing this you ll get one notification after 1min and second one after 2 min. In "didReceiveLocalNotification" method you can check the notification type and can show alert msg.
Hope this will help you.
Yes you can use the multiple local notification in your application.
Check this link.
Hope it helpfull for you

Setting local notification for a future date for Eastern time zone

I'm working on adding local notifications to an app I'm developing. I'm setting just one notification for 11:00pm on April 30, 2013 NY/Eastern time. How would I do this? I've tried multiple methods but none of them have worked correctly. This is what I'm using at the moment (it doesn't work):
- (void)applicationDidEnterBackground:(UIApplication *)application
{
if (![#"1" isEqualToString:[[NSUserDefaults standardUserDefaults]
objectForKey:#"setNotify"]]) {
[[NSUserDefaults standardUserDefaults] setValue:#"1" forKey:#"setNotify"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSString *str = [NSString stringWithFormat:#"2013-04-23T18:22:00"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"US/Eastern"]];
NSDate *dte = [dateFormat dateFromString:str];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[cal setTimeZone:[NSTimeZone timeZoneWithName:#"US/Eastern"]];
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
init];
if (notifyAlarm)
{
notifyAlarm.fireDate = dte;
notifyAlarm.timeZone = [NSTimeZone timeZoneWithName:#"US/Eastern"];
notifyAlarm.repeatInterval = 0;
notifyAlarm.soundName = #"trumpet.m4a";
notifyAlarm.alertBody = #"Message";
[app scheduleLocalNotification:notifyAlarm];
}
}
}
Try the following code:
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:30];
[dateComps setMonth:4];
[dateComps setYear:2013];
[dateComps setHour:23];
[dateComps setMinute:0];
[dateComps setSecond:0];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone timeZoneWithName:#"US/Eastern"];
localNotif.alertBody = #"Message";
localNotif.repeatInterval = 0;
localNotif.soundName = #"trumpet.m4a";
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
One thing I notice is that I doubt you meant to use YYYY in your date formatter. From Apple Docs
A common mistake is to use YYYY. yyyy specifies the calendar year whereas
YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar.
In most cases, yyyy and YYYY yield the same number, however they may
be different. Typically you should use the calendar year.
When I changed this your code worked for me and posted a notification.

Manage Multiple UILocal Notification

I have created multiple UI local Notifications , say 100. I have an array of UILocalNotifications which are scheduled as:
-(void) scheduleNotification{
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self.notifications removeAllObjects];
for (int i = 0; i< [delegate.viewController.contactList count] ; i++) {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSString *name = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:NAME_KEY];
NSString *birthday = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:BIRTHDAY_KEY];
if (birthday) {
[formatter setDateFormat:#"MM/dd/yyyy"];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date = [formatter dateFromString:birthday];
if (date == nil) {
[formatter setDateFormat:#"MM/dd"];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
date = [formatter dateFromString:birthday];
}
NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsEnd = [gregorianEnd components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date];
componentsEnd.year = [[NSDate date] year];
date = [gregorianEnd dateFromComponents:componentsEnd];
self.alarmTime = [date dateByAddingTimeInterval:self.mTimeInterval];
localNotification.fireDate = _alarmTime;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = 1;
NSString *itemName = #"B'day Alert!!!";
NSString *msgName = [NSString stringWithFormat:#"Celebrate %#'s B'day",name];
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:itemName,MessageKey, msgName,TitleKey, nil];
localNotification.userInfo = userDict;
localNotification.soundName = self.soundName;
localNotification.alertBody = [NSString stringWithFormat:#"Celebrate %#'s B'day",name];
[self.notifications addObject:localNotification];
}
}
}
}
I have implemented my applicationDidEnterBackground delegate as:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UILocalNotification* notification;
for (int i = 0; i< [self.settingVC.notifications count]; i++) {
notification = [self.settingVC.notifications objectAtIndex:i];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
Also, in didReceiveLocalNotification delegate I have this:
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
NSString *itemName = [notification.userInfo objectForKey:TitleKey];
NSString *messageTitle = [notification.userInfo objectForKey:MessageKey];
[self _showAlert:itemName withTitle:messageTitle];
application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
}
But the problem is that i'm not getting desired output.Any help would be appreciated. I know that at an application can have only a limited number of scheduled notifications; the system keeps the soonest-firing 64 notifications and discards the rest. So, how I will handle those 100 or more UILocalNotification?

how to store selected value and show on cell when application run again in iphone

I have a date picker view controller where I select the date. When I come back I can see the selected date on the tableview cell. So far, this all works properly. I face a problem when I exit the application and run it again, I can't see the last selected value in the cell. How can I do this?
If the user exits the application, they should be able to see the last selected date on the cell so that they can review or use that date.
This is my date controller class code .m file
- (void)viewDidLoad
{
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 0, 0)];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.minimumDate = [NSDate date];
[self.view addSubview:datePicker];
[super viewDidLoad];
}
-(void)DateChangeForFinalPayMent
{
//remove time
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
int comps = NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *dateComponents = [gregorian components:comps fromDate:[datePicker date]];
NSDate *dateSelected = [gregorian dateFromComponents:dateComponents];
NSLog(#"dateSelected:%#",dateSelected);
[gregorian release];
if ([selectionData type] == 0)
[selectionData setFromDateSelected:dateSelected];
else
[selectionData setToDateSelected:dateSelected];
}
-(IBAction)click
{
[self DateChangeForFinalPayMent];
[self.navigationController popViewControllerAnimated:YES];
}
here is my firstview controller where i show my selected value date on cell
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString* fromDate = [dateFormat stringFromDate:app.selectionData.fromDateSelected];
NSString* toDate = [dateFormat stringFromDate:app.selectionData.toDateSelected];
if ([indexPath row] == 0 && [indexPath section] == 1)
{
cell.textLabel.text=#"From Date";
cell.detailTextLabel.text=fromDate;
}
if ([indexPath row] == 1 && [indexPath section] == 1)
{
cell.textLabel.text=#"To Date";
cell.detailTextLabel.text=toDate;
}
Can anybody suggest how to store the value and show it to the user when they open the application again.
PFB code to save and reteive the date value from the User defaults
//Storing date value to the user defaults
NSDate * selectedDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Set the date format.. u can choose different one based on ur requirement
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//String from the NSDate
NSString *pStrDate = [dateFormatter stringFromDate:selectedDate];
//Save date value to the user defaults
[[NSUserDefaults standardUserDefaults] setObject: pStrDate forKey:#"date"];
[[NSUserDefaults standardUserDefaults] synchronize];
Retrieving date from the user defaults:
NSDate *dateFromString = [[NSDate alloc] init];
NSString *savedDateVaue=nil;
if([[NSUserDefaults standardUserDefaults] objectForKey:#"date"]!=nil)
{
savedDateVaue=[[NSUserDefaults standardUserDefaults] objectForKey:#"date"];
dateFromString = [dateFormatter dateFromString:savedDateVaue];
}