-1

I am looking to add a reminder feature to my application using UILocalNotification however there is a limit of just 64 registered notifications and with my app I have calculated that users may go beyond that limit.

Therefore I am now looking at recording the users reminder settings and to create a queue of future notifications stored in core data.

Each time the application is launched it would check how many Notifications are registered with UILocalNotification and if the number is < 40 it will allocate the next 24 scheduled notifications from core data to top it back up to 64.

I'm currently struggling on how to work out the exact Dates which could occur between now and the next 3 months and I am only interested in dates which are the selected day i.e. Wednesday.

Thanks in advance.

Aaron

MonkeyBlue
  • 2,234
  • 6
  • 31
  • 41
  • 3
    So, did you have a question, or do you want us to look at the documentation for NSCalendar and NSDateComponents and tell you what it says? – Hot Licks Oct 30 '14 at 15:25
  • 1
    Use `NSCalendar` method `dateByAddingComponents:toDate:options:` to get the next `NSDate`, and repeat until you have the desired number of new dates. – Rob Oct 30 '14 at 15:43

1 Answers1

1

Thanks to the others for the kick I needed to read the documentation heres what I ended up doing with a little help.

// Current Date + Time given for initial reminder
NSString *sDateGiven=@"28-10-2014 2:15:00 PM";

int dayOfWeekToRemind = 4; // Wednesday

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:@"dd-MM-yyyy h:mm:ss a"];


// Convert to NSDate
NSDate *alarmDate = [formatter dateFromString:sDateGiven];

NSCalendar *calendar = [NSCalendar currentCalendar];



// Now we need to work out Next Wednesday
NSDateComponents *componentsForFireDate1 = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfYearCalendarUnit) fromDate: alarmDate];

[componentsForFireDate1 setWeekday: dayOfWeekToRemind] ; // Set to Wednesday

// Check if the date given is before or after wednesday as we are only interested in future Wednesdays

if ([componentsForFireDate1 weekday] > dayOfWeekToRemind) {
    // If greater we add a week
    [componentsForFireDate1 setWeekOfYear: [componentsForFireDate1 weekOfYear] + 1];
}

// Now we have the very first start date for the next Wednesday
alarmDate = [calendar dateFromComponents:componentsForFireDate1];

int i = 0;

// Loop through 30 weeks
for (i = 0; i < 30; i++) {

    NSDateComponents *componentsForFireDate = [[NSDateComponents alloc] init];

    [componentsForFireDate setWeekday: 7] ; // Add 7 days

    alarmDate = [calendar dateByAddingComponents:componentsForFireDate toDate:alarmDate options:0];
    NSLog(@"Date = %@", alarmDate);
}

Then then outputs as follows:

Given Date Date 2014-10-28 14:15:00 +0000
ADJUST DATED 2014-10-29 14:15:00 +0000
Date = 2014-11-05 14:15:00 +0000
Date = 2014-11-12 14:15:00 +0000
Date = 2014-11-19 14:15:00 +0000
Date = 2014-11-26 14:15:00 +0000
Date = 2014-12-03 14:15:00 +0000
Date = 2014-12-10 14:15:00 +0000
Date = 2014-12-17 14:15:00 +0000
Date = 2014-12-24 14:15:00 +0000
Date = 2014-12-31 14:15:00 +0000
MonkeyBlue
  • 2,234
  • 6
  • 31
  • 41