2013-08-21 37 views
3

我試圖將事件添加到日曆復發規則RRULE沒有添加:= YEARLY FREQ; BYMONTH = 6,7; BYDAY = [帖EKEvent根據給定EKRecurrenceRule

所以根據這個規則事件應該每年添加一次,每個6月1日星期四和7月直到到期日期,這是我在我的項目中設置的。

在我的項目中,創建事件,但不是根據重複規則。通過以下代碼,活動僅在每年的6月1日星期四添加。爲什麼這些事件不會在每個7月的第一個星期四增加?

這裏是.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]; 
} 

有人可以幫我對此?

+0

請更具體 - 當創建事件時?另外,在這裏包含_relevant_代碼,而不是鏈接到現場。 –

+0

我正在嘗試使用重複規則添加事件。但是事件重複根據重複規則不起作用。對我來說這很奇怪,可能是我想念一些東西。這就是我添加項目鏈接的原因。 – shuvo

+0

@shuvo與此有關的任何消息?我跑了多個測試,看起來'monthsOfTheYear'被簡單地忽略了。另外,當從'EventKit'讀取時,'monthsOfTheYear'總是零,即使在2個不同的月份創建了事件(從mac osx日曆創建) –

回答

1

這似乎是另一個問題的重複。基本上,根據「BYDAY」規則,年度第一頻率意味着一年中的第一週 - 而不是每個月的第一週。

@Shuvo,我沒有閱讀rfc。但這裏是Apple文檔EKRecurrenceDayOfWeek

EKRecurrenceDayOfWeek類表示用於EKRecurrenceRule對象的星期幾。一週中的某天可以有一個星期的數字,表示重複規則的頻率中的特定日期。例如,星期幾值爲星期二,星期數爲2的星期幾將表示每月重複規則中每月的第二個星期二以及每年重複規則中每年的第二個星期二。

當你說「第一個星期四」時,這是正確的 - 除了在每年的情況下,它是今年的第一個星期四。

+0

我覺得你誤解了,經常性的規則是關於互聯網日曆和調度核心對象規範,請閱讀這個http://www.ietf.org/rfc/rfc2445.txt鏈接瞭解更多細節,1TH表示每週一的第一個星期四。 – shuvo

0

這個錯誤得到了Apple的證實,至少在iOS 7.1.3(這是目前最新版本)之前。