tony.stack tony.stack - 4 months ago 26
iOS Question

Setting Reminder with a dueDate with Recurrence Rule iOS

Question: How do I properly set my reminder due date since I have a recurrence rule?

Here is what the reminder object looks like:

EKReminder <0x1700cf490> {title = Dickens's CANINE GOLD WELLNESS doses[1.00]; **dueDate = (null)**; **completionDate = (null)**; priority = 0; calendarItemIdentifier = D1D99FEA-2BFA-4DB1-9D86-7FB26246B50A; alarms = (
"EKAlarm <0x1780a9420> {triggerInterval = -79200.000000}"
)}


The error I am getting is:

Reminder Error=[A repeating reminder must have a due date.]


You can see in the code that I am fooling around with NSDateComponents as a solution since startDateComponents which I just set the month/day/year and local timezone of the reminder which will produce an all day reminder, which in this case is fine. I will probably move the date components and setting of the due date inside the recurrence section when it is done.

Here is my code:

-(void)setReminders:(NSString *)reminderText
andDate:(NSString *)reminderdate
andPetName:(NSString*)petName
andDose:(NSNumber *)dose {

EKEventStore *store = [[EKEventStore alloc] init];

NSDate * reminderNewDate = [self getDateFromString:reminderdate];
petName = [ConfigOps readProperty:kConfigOpsPetKey];

NSString *reminderTitle = [NSString stringWithFormat:@"%@'s %@", petName, reminderText];
NSUInteger doseCount = 0;

if ([dose integerValue] != 0 || dose != nil) {
doseCount = [dose integerValue];
}
else{
doseCount = 0;//NOTE: looks like purchases will have doses not reminders so set to 0 for now.
}

[store requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
// access code here
EKReminder *new_reminder = [EKReminder reminderWithEventStore:store];
new_reminder.title = reminderTitle;

new_reminder.calendar = store.defaultCalendarForNewEvents;

//get the date components
NSDateComponents *comp = [[NSDateComponents alloc]init];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
[gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit |
NSWeekdayCalendarUnit) fromDate:reminderNewDate];
NSInteger day = [weekdayComponents day];
NSInteger month = [weekdayComponents month];
//NSInteger weekday = [weekdayComponents weekday];//future reference
NSInteger year = [weekdayComponents yearForWeekOfYear];
//Month is dose+month = end of reccurence
month = month + doseCount;
[comp setYear:year];
[comp setMonth:month];
[comp setDay:day];
NSDate *date = [gregorian dateFromComponents:comp];

NSTimeZone *myNSTimeZone = gregorian.timeZone;
NSDateComponents *start = new_reminder.startDateComponents;
start.timeZone = myNSTimeZone;

start.month = [weekdayComponents month];
start.day = [weekdayComponents day];
start.year = [weekdayComponents yearForWeekOfYear];

new_reminder.startDateComponents = start;
new_reminder.dueDateComponents = start;
new_reminder.completed = NO;

//Create alarm 22 hours before
double alarmAmountInSeconds = 60.0*60.0*22.0;
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:(-1.0*alarmAmountInSeconds)];

[new_reminder addAlarm:alarm];
//new_reminder.alarms = [NSArray arrayWithObject:alarm];

//create nice text for note.
//Hey there! petName needs remindertext from your friendly clinic, clinicName!
new_reminder.notes = reminderText;

if (doseCount != 0) {

EKRecurrenceRule *recurranceRule = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyMonthly
interval:1
end:[EKRecurrenceEnd recurrenceEndWithOccurrenceCount:doseCount]
];
new_reminder.calendar = [store defaultCalendarForNewReminders];
[new_reminder addRecurrenceRule:recurranceRule];
}

NSError *er;
//EKEventEditViewController
BOOL success = [store saveReminder:new_reminder commit:YES error:&er];

if (success) {
// Handle here
NSString *alertMessage = [NSString stringWithFormat:@"Reminder Created for\n%@", reminderTitle];
NSString *alertTitle = @"Please check your Reminders";
UIAlertView *alertR = [[UIAlertView alloc]initWithTitle: alertTitle
message: alertMessage
delegate: self
cancelButtonTitle:nil
otherButtonTitles:@"OK",nil];
[alertR show];

}
else{
//log error
NSLog(@" Reminder Error=[%@]", [er localizedDescription]);
//log to error table in database &inform Flurry?
}
}];
}


The method works if there is no recurrence set since it doesn't require a start date/due date.

Answer

After some fixing of the date (I found was returning nil), I found that I have to set and end recurrence rule when adding a dose amount.

Here is the code which gets rid of the error (which is pretty funny of Apple to have - kudos Apple!).

if (doseCount != 0) {

            EKRecurrenceRule *recurranceRule = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyMonthly
                                                                                            interval:1
                                                                                                 end:[EKRecurrenceEnd recurrenceEndWithOccurrenceCount:doseCount]
                                                ];
            new_reminder.calendar = [store defaultCalendarForNewReminders];

            //FIX for : recuurence end - Reminder Error = [A repeating reminder must have a due date.]
            EKRecurrenceEnd *endRec = [EKRecurrenceEnd recurrenceEndWithEndDate:date];
            EKRecurrenceRule *recur = [[EKRecurrenceRule alloc]initRecurrenceWithFrequency:EKRecurrenceFrequencyDaily interval:   1 end:endRec];

            unsigned unitFlags= NSYearCalendarUnit|NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit |NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit;

            NSDateComponents *dailyComponents=[gregorian components:unitFlags fromDate:date];
            [new_reminder setDueDateComponents:dailyComponents];
            [new_reminder addRecurrenceRule:recur];

            //add it.
            [new_reminder addRecurrenceRule:recurranceRule];

        }

Hope this helps someone get through this.

Comments