1

I have an array of dates

NSArray *datesArray=[NSArray arrayWithObjects: @"2014-09-14 00:00:00",@"2014-08-21 07:12:36",@"2014-08-14 00:00:00",@"2014-07-14 00:00:00",@"2014-06-14 00:00:00",@"2015-01-01 10:00:00",@"2014-06-14 00:00:00",@"2014-05-14 11:24:15", nil];

now i want to fire one day before the date available in array How to implement it?

I was trying this

    NSDateFormatter *Formatter1 = [[NSDateFormatter alloc] init];
    [Formatter1 setLocale:[[NSLocale alloc] initWithLocaleIdentifier:NSLocaleIdentifier]];
    [Formatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    [Formatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    NSDate *date1 =[NSDate date];
    NSString *string =[Formatter1 stringFromDate:date1];
    NSLog(@"sring %@",string);
    NSDate *todaydate =[Formatter1 dateFromString:string];
    NSLog(@"2day date is %@",todaydate);

for (int i=0;i<datesArray.count;i++)
{
    NSDate *_date =[Formatter1 dateFromString:[datesArray objectAtIndex:i ]];
    NSLog(@"date is %@",_date);
    if(_date == todaydate){
        localNotif.fireDate = _date;
        localNotif.alertBody = @"festival";
        localNotif.alertAction=@"Show me";
        localNotif.applicationIconBadgeNumber = 1;
        localNotif.soundName = UILocalNotificationDefaultSoundName;

        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
biki
  • 35
  • 4
  • A small note on code style - change `Formatter1` to `formatter1`. Classes should start with uppercase letters and variables with lowercase letters. This will improve your code's clarity. – Guy Kogus May 14 '14 at 12:46

1 Answers1

2

You can subtract a day by using the NSCalendar functions, specifically dateByAddingComponents

NSArray *datesArray = @[@"2014-09-14 00:00:00",@"2014-08-21 07:12:36",@"2014-08-14 00:00:00",@"2014-07-14 00:00:00",@"2014-06-14 00:00:00",@"2015-01-01 10:00:00",@"2014-06-14 00:00:00",@"2014-05-14 11:24:15"];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setDay:-1];

NSDateFormatter *Formatter1 = [[NSDateFormatter alloc] init];
[Formatter1 setLocale:[[NSLocale alloc] initWithLocaleIdentifier:NSLocaleIdentifier]];
[Formatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[Formatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

[datesArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = [calendar dateByAddingComponents:dateComponents toDate:[Formatter1 dateFromString:obj] options:0];
    localNotif.alertBody = @"festival";
    localNotif.alertAction = @"Show me";
    localNotif.applicationIconBadgeNumber = 1;
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}];

W

William George
  • 6,735
  • 3
  • 31
  • 39
  • :Thank you for answer..whenever i run the app local notification pop up..also it will pop up exact 1 day earlier? – biki May 15 '14 at 04:12
  • Indeed. Look into cancelling notifications, you can do this by using `cancelAllLocalNotifications`. If you have a reference to the notification you can cancel a specific one: `cancelLocalNotification:`. You can get the reference by using `scheduledLocalNotifications` - This will let you know all the notifications that have been scheduled. You could even check if there is an existing notification with the same date before you add another one using this method. – William George May 15 '14 at 08:50
  • fine thank u..one more question..if above datesarray are the json object and i want to pull it from json service for UILocalNotification – biki May 15 '14 at 09:12
  • `[NSJSONSerialization JSONObjectWithData:self.jsonData options:0 error:NULL]` will return a dictionary that you can call `objectForKey:` on. – William George May 15 '14 at 09:44
  • ohk u mean i parse json and then it can dynamically communicate with UILocalNotification giving notification for particular events in the specified dated for that events.So parsing can be done in applicationDidEnterBackground method because i want notification when app is in background state. – biki May 15 '14 at 09:57
  • Make sure your app is registered for background modes. If not you will not get much processing time in did enter background. – William George May 15 '14 at 13:05