0

I have this method which adds events to native iphone calendar. It is already adding monthly reminders successfully - but I want to force any monthy reminders to fall into week days (not weekends).

The NSDictionary model is simply Id: Start_Date__c Finish_Date__c Payment_Interval__c = Monthly

- (void)addRecurringEventsForPartnership:(NSDictionary *)dict{
    ENTER_METHOD;

    NSError *error = nil;
    EKEvent *startEvent  = [EKEvent eventWithEventStore:self.eventStore];
    startEvent.calendar = self.defaultCalendar;
     startEvent.availability = EKEventAvailabilityFree;
    startEvent.startDate = [NSDate dateWithLongFormatString:[dict valueForKey:@"Start_Date__c"]];
    startEvent.allDay = YES;
   // startEvent.endDate =  [startEvent.startDate dateByAddingTimeInterval:30*60];
     startEvent.title = [dict theNameValue];
    //http://stackoverflow.com/questions/7718006/xcode-why-is-my-event-not-being-added-to-the-calendar
    if ([startEvent.startDate isEqualToDate:startEvent.endDate]) {
        startEvent.endDate = [startEvent.startDate dateByAddingTimeInterval:30*60];;
    }

   // if 
    if ([[dict valueForKey:@"Payment_Interval__c"] isEqualToString:@"Monthly"]) {
        EKRecurrenceFrequency freq = EKRecurrenceFrequencyMonthly;
        int recurrenceInterval = 1;

        EKRecurrenceRule *rule = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:freq interval:recurrenceInterval end:nil];
        startEvent.recurrenceRule = rule;

        startEvent.notes = [NSString stringWithFormat:@"Id:%@",[dict valueForKey:@"Id"]];
        // [self.eventStore removeEvent:startEvent span:EKSpanThisEvent error:&error];
        [self.eventStore saveEvent:startEvent span:EKSpanThisEvent error:&error];

        if (error != nil)
        {
            DLog(@"WARNING:%@",error.description);
            // TODO: error handling here
        }
    }



  //  DLog(@"startEvent.endDate:%@",startEvent.endDate);


    EKEvent *finishEvent  = [EKEvent eventWithEventStore:self.eventStore];
    finishEvent.calendar = self.defaultCalendar;
    finishEvent.availability = EKEventAvailabilityFree;
    finishEvent.startDate = [NSDate dateWithLongFormatString:[dict valueForKey:@"Finish_Date__c"]];
    finishEvent.allDay = YES;
    finishEvent.title = [NSString stringWithFormat:@"%@ - Finish",[dict theNameValue]];


    finishEvent.notes = [NSString stringWithFormat:@"Id:%@",[dict valueForKey:@"Id"]];
    [self.eventStore saveEvent:finishEvent span:EKSpanThisEvent error:&error];

    if (error != nil)
    {
        DLog(@"WARNING:%@",error.description);
        // TODO: error handling here
    }

}
johndpope
  • 5,035
  • 2
  • 41
  • 43
  • Did you got any working solution of this ? I been trying to edit the Monthly recurrence but only for the single day while rest of the day it will occur on the same time.How it can be achieved ? – Ajay Sharma Aug 30 '12 at 14:16

2 Answers2

1

Here's something that works (at least in iOS7, didn't test on other systems):

EKRecurrenceRule *er = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyMonthly interval:1
                                                                   daysOfTheWeek:@[[EKRecurrenceDayOfWeek dayOfWeek:2], // Monday
                                                                                   [EKRecurrenceDayOfWeek dayOfWeek:3], // Tuesday
                                                                                   [EKRecurrenceDayOfWeek dayOfWeek:4], // Wednesday
                                                                                   [EKRecurrenceDayOfWeek dayOfWeek:5], // Thursday
                                                                                   [EKRecurrenceDayOfWeek dayOfWeek:6]] // Friday
                                                                  daysOfTheMonth:@[@1, @2]
                                                                 monthsOfTheYear:nil weeksOfTheYear:nil daysOfTheYear:nil setPositions:nil end:nil];
event.recurrenceRules = @[er];
alex-i
  • 5,406
  • 2
  • 36
  • 56
1

Couldn't you use NSDateFormatter to get the numeric day of the week and then adjust by subtracting or adding 1 or 2 depending on which day it returned?

[dateFormatter setDateFormat:@"c"];

Will return numeric (1-7) day of the week, I believe

Jayson Lane
  • 2,828
  • 1
  • 24
  • 39