HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 根据给定的EKRecurrenceRule不添加EKEvent大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用递归规则RRULE将事件添加到日历中:FREQ = YEARLY; BymONTH = 6,7; BYDAY = 1TH

所以根据这个规则,事件应该每年添加一次,每年的6月1日和7月,直到到期日,我已经在我的项目中设置了.

在我的项目中,会创建事件,但不会根据重复规则创建事件.使用以下代码,事件仅在6月1日星期四添加.为什么每个7月1日星期四都没有添加活动呢?

这是.m文件代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view,typically from a nib.
    [self createEvent];
}

- (void)createEvent
{
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    EKEvent *event = [EKEvent eventWithEventStore:eventStore];
    event.title = @"testRecurrenceRule";
    event.LOCATIOn = @"Dhaka";
    [event setCalendar:[eventStore defaultCalendarForNewEvents]];
    event.startDate = [self dateFromString:@"2013-06-18T21:00:00+06:00"];
    event.endDate = [self dateFromString:@"2013-06-18T22:00:00+06:00"];

    id recurrenceRule = [self recurrenceRuleForEvent];
    if(recurrenceRule != nil)
        [event addRecurrenceRule:recurrenceRule];

    if ([eventStore respondsToSELEctor:@SELEctor(requestAccessToEntityType:completion:)])
    {
        // iOS 6 and later
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted,NSError *error) {
            if (granted)
            {
                dispatch_async(dispatch_get_main_queue(),^{
                    [self saveTheEvent:event eventStore:eventStore];
                    //[eventStore saveEvent:event span:EKSpanThisEvent error:error];
                });
            }
            else
            {
                dispatch_async(dispatch_get_main_queue(),^{

                    //do nothing
                });
            }
        }];
    }
    else
    {
        [self saveTheEvent:event eventStore:eventStore];
    }

    textView.text = [NSString StringWithFormat:@"Event has been added with recurrence rule %@",recurrenceRule];
}

- (void)saveTheEvent:(EKEvent *)aEvent eventStore:(EKEventStore *)aStore
{
    [aStore saveEvent:aEvent span:EKSpanThisEvent error:NULL];
}

- (EKRecurrenceRule *)recurrenceRuleForEvent
{
    //just creaTing a recurrence rule for RRULE:FREQ=YEARLY;BymONTH=6,7;BYDAY=1TH
    // setTing the values directly for tesTing purpose.

    //FREQ=YEARLY
    EKRecurrenceFrequency recurrenceFrequency = EKRecurrenceFrequencyYearly;
    NSInteger recurrenceInterval = 1;                                             
    EKRecurrenceEnd *endRecurrence = nil;                                         
    NSMutableArray *monthsOfTheYearArray = [NSMutableArray array];               
    NSMutableArray *daysOfTheWeekArray = [NSMutableArray array];                
    NSMutableArray *daysOfTheMonthArray = [NSMutableArray array];               
    NSMutableArray *weeksOfTheYearArray = [NSMutableArray array];              
    NSMutableArray *daysOfTheYearArray = [NSMutableArray array];          
    NSMutableArray *setPositionsArray = [NSMutableArray array];         

    //BymONTH=6,7
    [monthsOfTheYearArray addObject:[NSnumber numberWithInt:6]];
    [monthsOfTheYearArray addObject:[NSnumber numberWithInt:7]];

    //BYDAY=1TH
    [daysOfTheWeekArray addObject:[EKRecurrenceDayOfWeek dayOfWeek:5 weeknumber:1]];

    endRecurrence = [EKRecurrenceEnd recurrenceEndWithEndDate:[self dateFromString:@"2018-12-15T22:30+06:00"]];

    EKRecurrenceRule *recurrence = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:recurrenceFrequency
                                                                                interval:recurrenceInterval
                                                                           daysOfTheWeek:daysOfTheWeekArray
                                                                          daysOfTheMonth:daysOfTheMonthArray
                                                                         monthsOfTheYear:monthsOfTheYearArray
                                                                          weeksOfTheYear:weeksOfTheYearArray
                                                                           daysOfTheYear:daysOfTheYearArray
                                                                            setPositions:setPositionsArray
                                                                                     end:endRecurrence];
    return recurrence;
}

- (NSDate *)dateFromString:(NSString *)String
{
    //check if the date String in null
    if ([String length] == 0)
        return nil;

    NSString *dateString = nil;
    NSString *modifiedString = nil;
    BOOL secSpotMissing = false;

    NSRange range = [String rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersinstring:@"T"]];
    if (range.LOCATIOn != NsnotFound)
    {
        dateString = [String subStringFromIndex:range.LOCATIOn];

        range = [dateString rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersinstring:@"+-Z"]];
        if (range.LOCATIOn != NsnotFound)
        {
            //seperate the time portion of date String and checking second field is missing or not. like is it HH:mm or HH:mm:ss?
            if ([[[dateString subStringToIndex:range.LOCATIOn] componentsSeparatedByString:@":"] count] < 3)
                secSpotMissing = true;

            //seperate the time zone portion and checking is there any extra ':' on it. It should like -0600 not -06:00. If it has that extra ':',just replacing it here.
            dateString = [dateString subStringFromIndex:range.LOCATIOn];
            if([dateString hasSuffix:@"Z"])
                modifiedString = [dateString StringByreplacingoccurrencesOfString:@"Z" withString:@"+0000"];
            else
                modifiedString = [dateString StringByreplacingoccurrencesOfString:@":" withString:@""];
            String = [String StringByreplacingoccurrencesOfString:dateString withString:modifiedString];
        }
    }
    else
        return nil;

    // converTing the date String according to it's format.
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    if (secSpotMissing)
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mmZZZ"];
    else
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
    return [dateFormatter dateFromString:string];
}

有人可以帮我解决这个问题吗?

解决方法

这似乎是另一个问题的重复.基本上,根据“BYDAY”规则,YEARLY频率的第1周意味着一年中的第一周 – 而不是每个月的第一周.

@Shuvo,我没看过rfc.但这里是Apple文档EKRecurrenceDayOfWeek.

当你说“第一个星期四”时,这是正确的 – 除了在每年的背景下,它是一年的第一个星期四.

大佬总结

以上是大佬教程为你收集整理的ios – 根据给定的EKRecurrenceRule不添加EKEvent全部内容,希望文章能够帮你解决ios – 根据给定的EKRecurrenceRule不添加EKEvent所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。