iOS日历重复事件

时间:2014-05-26 12:58:25

标签: ios calendar ekevent ekeventstore

美好的一天, 我无法在标准iOS日历中添加将重复的自定义事件(例如2个月内)。

问题是我的活动直到当月结束。它将在哪一天开始并不重要 - 它将在本月重复。 代码:

EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) 
{
    if (!granted)  
        return; 
    // setting the values directly for testing purpose.
    EKRecurrenceEnd *endRecurrence = nil;
    NSMutableArray *daysOfTheWeekArray = [NSMutableArray array];

    //Only weekends
    [daysOfTheWeekArray addObject:[EKRecurrenceDayOfWeek dayOfWeek:6]];
    [daysOfTheWeekArray addObject:[EKRecurrenceDayOfWeek dayOfWeek:7]];

    endRecurrence = [EKRecurrenceEnd recurrenceEndWithEndDate:[[NSDate date] dateByAddingTimeInterval:60*60*24*7*10]]];

    //create the recurrence rule here
    EKRecurrenceRule *recurrence = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency: EKRecurrenceFrequencyYearly
                                                                                interval:1
                                                                           daysOfTheWeek:daysOfTheWeekArray
                                                                          daysOfTheMonth:nil
                                                                         monthsOfTheYear:nil
                                                                          weeksOfTheYear:nil
                                                                           daysOfTheYear:nil
                                                                            setPositions:nil
                                                                                     end:endRecurrence];

    EKEvent *event = [EKEvent eventWithEventStore:store];
    event.title = @"Title";
    event.startDate = [NSDate date]; //today
    event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
    [event addRecurrenceRule: recurrence];

    [event setCalendar:[store defaultCalendarForNewEvents]];
    NSError *err = nil;
    [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}]; 

1 个答案:

答案 0 :(得分:0)

请使用EKRecurrenceFrequencyMonthly更改您的频率,目前为EKRecurrenceFrequencyYearly

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

[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) 
{
    if (!granted)  
    return;

    // setting the values directly for testing purpose.
    EKRecurrenceEnd *endRecurrence = nil;
    NSMutableArray *daysOfTheWeekArray = [NSMutableArray array];

    //Only weekends
    [daysOfTheWeekArray addObject:[EKRecurrenceDayOfWeek dayOfWeek:6]];
    [daysOfTheWeekArray addObject:[EKRecurrenceDayOfWeek dayOfWeek:7]];

    endRecurrence = [EKRecurrenceEnd recurrenceEndWithEndDate:[[NSDate date] dateByAddingTimeInterval:60*60*24*7*10]]];

    //create the recurrence rule here
    EKRecurrenceRule *recurrence = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency: EKRecurrenceFrequencyMonthly
                                                                            interval:1
                                                                       daysOfTheWeek:daysOfTheWeekArray
                                                                      daysOfTheMonth:nil
                                                                     monthsOfTheYear:nil
                                                                      weeksOfTheYear:nil
                                                                       daysOfTheYear:nil
                                                                        setPositions:nil
                                                                                 end:endRecurrence];

    EKEvent *event = [EKEvent eventWithEventStore:store];
    event.title = @"Title";
    event.startDate = [NSDate date]; //today
    event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
    [event addRecurrenceRule: recurrence];

    [event setCalendar:[store defaultCalendarForNewEvents]];
    NSError *err = nil;
    [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
}];