0

I wrote an app that allows users to enter in a date on a date picker, and the app will alert the user 36 hours before that date arrives. My issue is in the dateFromString:. I'm not sure what to put in there. Here is the code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm'/'dd'/'yyyy"];
NSDate *eventDate=[dateFormatter dateFromString: ]; 

localNotif.fireDate = [eventDate dateByAddingTimeInterval:-13*60*60];
localNotif.timeZone = [NSTimeZone defaultTimeZone];


localNotif.alertBody = @"Event tommorow!";

localNotif.alertAction = nil;

localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];    

return YES;

}

Any help is much appreciated, thank you!

Edit: Here is some additional code that I'm using to save the date they entered on the date picker if that helps:

- (void)viewDidLoad {
    NSDate *storedDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"DatePickerViewController.selectedDate"];

    [self.datePicker setDate:storedDate animated:NO];
}

And this is the method that saves the data:

- (IBAction)dateChanged:(id)sender {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSDate *selectedDate = [self.datePicker date];

[defaults setObject:selectedDate forKey:@"DatePickerViewController.selectedDate"];}
Henry F
  • 4,960
  • 11
  • 55
  • 98
  • You have give the event date as string...it will convert into date itself...in this format @"mm//dd/yyyy"... – Dinesh Raja Jan 20 '12 at 04:48
  • Oh ok, thank you. The user enters in the date themselves, which is what makes it tricky for me. Do you know how I would go about getting the date they entered, and setting it in the `dateFromString` area? – Henry F Jan 20 '12 at 04:52
  • Sorry i dont get it...i dont know that too exactly.. – Dinesh Raja Jan 20 '12 at 04:56

1 Answers1

2

Why do you need a string? After making

[defaults setObject:selectedDate forKey:@"DatePickerViewController.selectedDate"];

you can access your date just like

NSDate* eventDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"DatePickerViewController.selectedDate"];

Using strings is not so nice with date - you should care about locale and time formats (12 hours or 24 hours) etc.

Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84
  • Oh awesome, thank you so much. So, should I just delete the `NSDate *eventDate=[dateFormatter dateFromString: ];` and switch it to `NSDate* eventDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"DatePickerViewController.selectedDate"];`? – Henry F Jan 20 '12 at 05:08
  • 1
    Yes, just add checking for nil. First time you'll launch your app on the device there will be no stored date in userDefaults. – Pavel Oganesyan Jan 20 '12 at 05:44
  • Awesome thanks, yeah that may be why my app crashes when I click on the tab with the date picker. How do I check for nil? Sorry for the beginner question. – Henry F Jan 20 '12 at 06:04
  • `NSDate* eventDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"DatePickerViewController.selectedDate"]; if (eventDate!=nil) { //Your code here }` Sorry for bad formatting, it's just comment. – Pavel Oganesyan Jan 20 '12 at 06:45
  • Thank you so much for all your help! You helped me out tremendously! – Henry F Jan 20 '12 at 06:51
  • If (eventDate == nil) and you want to get current time, use `eventDate = [NSDate date];` [NSDate description](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html) – Pavel Oganesyan Jan 20 '12 at 06:53
  • @Павел Оганесян Hey, I have a question about the objectForKey: area. What should I put in there, the name of my nib file? Thank you. – Henry F Jan 22 '12 at 03:28
  • 1
    It should be the same key that you've put in a `[defaults setObject:selectedDate forKey:@"DatePickerViewController.selectedDate"];` It is just a string that helps you to identify objects you've stored in userDefaults. – Pavel Oganesyan Jan 22 '12 at 09:33
  • @Павел Оганесян Awesome thank you. I tried that but it still doesn't seem to work. I created another thread about it here: http://stackoverflow.com/questions/8958898/iphone-local-notifications-wont-appear/ – Henry F Jan 22 '12 at 17:17