2013-10-21 52 views
1

我正在向IOS日曆添加事件數組。因爲它必須單擊按鈕纔會發起攻擊。通過這樣做,所有事件都會一次又一次地添加。所以過度過來,我用NSPredicate得到所有的事件,並做驗證像下面檢查重複事件,但不在IOS中的事件存儲區中獲取所有事件

NSDate *edate=[formatter dateFromString:startDateString]; 
NSDate *fdate=[formatter dateFromString:endDateString]; 


NSLog(@"Start Date is %@",edate); 
NSLog(@"End Date is %@",fdate); 

event1.startDate =edate; 
event1.endDate=fdate; 


NSArray *caleandarsArray = [[NSArray alloc] init]; 
caleandarsArray = [eventStore calendars] ; 

NSPredicate *predicateForEvents = [eventStore predicateForEventsWithStartDate:edate endDate:fdate calendars:caleandarsArray]; // nil will search through all calendars 


NSArray *eventsOnDate = [eventStore eventsMatchingPredicate:predicateForEvents]; 

NSLog(@"Events on holday date is %@",eventsOnDate); 


BOOL eventExists = NO; 

for (EKEvent *eventToCheck in eventsOnDate) 
{ 

    NSLog(@" eventToCheck title is %@",eventToCheck.title); 
    NSLog(@" myTripsArray title is %@",[myTripsArray objectAtIndex:i]); 

    if ([eventToCheck.title isEqualToString:[myTripsArray objectAtIndex:i]]) 
    { 
      eventExists = YES; 
     NSLog(@"Event already existed"); 

    } 
    } 



    if (eventExists == NO) 
     { 

     [event1 setTimeZone:[NSTimeZone systemTimeZone]]; 
     [event1 setCalendar:[eventStore defaultCalendarForNewEvents]]; 
     NSError *err; 
     if(err) 
     { 
      NSLog(@"Error occured"); 

     } 
      else 
     { 
      NSLog(@"Error not occured"); 

     } 
       [eventStore saveEvent:event1 span:EKSpanThisEvent error:&err]; 
     } 

} 

通過上面的代碼,我只得到了生日事件不被存在的任何其他活動。如何從日曆中獲取所有事件。

請幫我解決我出錯的地方。

回答

3

我解決它通過使用下面的代碼

-(IBAction)calenderButtonAction:(id)sender 
{ 

eventStore = [[EKEventStore alloc] init]; 
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) 
{ 
    // the selector is available, so we must be on iOS 6 or newer 
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) { 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      if (error) 
      { 
       // display error message here 
       NSLog(@"error"); 


      } 
      else if (!granted) 
      { 
       // display access denied error message here 
       NSLog(@"not granted"); 
      } 
      else 
      { 
       // access granted 
       NSLog(@" granted"); 
       [self addingDetailsToCalender]; 

       // ***** do the important stuff here ***** 
      } 
     }); 
    }]; 
} 
} 

添加事件被忽略重複日曆

-(void)addingDetailsToCalender 
{ 

EKEvent *event1 = [EKEvent eventWithEventStore:eventStore]; 
[email protected]"Hyderabad"; 
[email protected]"Sample test Satya "; 

event1.startDate =[NSDate date]; 
// event1.endDate=[NSDate dateWithTimeIntervalSinceNow:86400];; 
NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSDateComponents *comps = [NSDateComponents new]; 
comps.day = 7; 
NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0]; 

NSLog(@"Seven days date is %@",sevenDays); 
event1.endDate=sevenDays; 

    NSPredicate *predicateForEvents = [eventStore predicateForEventsWithStartDate:[NSDate date] endDate:[NSDate dateWithTimeIntervalSinceNow:10*365*86400] calendars:nil]; 

NSArray *eventsOnDate = [eventStore eventsMatchingPredicate:predicateForEvents]; 

NSLog(@"Events on holday date is %@",eventsOnDate); 


BOOL eventExists = NO; 

for (EKEvent *eventToCheck in eventsOnDate) 
{ 

    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init]; 
    [dateFormatter setDateFormat:@"yyyy-MM-dd"]; 
    NSString *startDateStr=[NSString stringWithFormat:@"%@", 
          [dateFormatter stringFromDate:eventToCheck.startDate]]; 
    NSString *event1startDate= [NSString stringWithFormat:@"%@", [dateFormatter stringFromDate:event1.startDate]]; 
    NSLog(@"mystart date is %@",startDateStr); 
    NSLog(@"my event start date is %@",event1startDate); 


     if ([eventToCheck.title isEqualToString:event1.title] && [startDateStr isEqualToString:event1startDate]) 
    { 
    eventExists = YES; 
    NSLog(@"Event already existed"); 

    } 
} 


if (eventExists == NO) 
{ 

    [event1 setTimeZone:[NSTimeZone systemTimeZone]]; 
    [event1 setCalendar:[eventStore defaultCalendarForNewEvents]]; 
    NSError *err; 
if(err) 
{ 
    NSLog(@"Error occured"); 

} 
    else 
{ 

    NSLog(@"Error not occured"); 

} 
[eventStore saveEvent:event1 span:EKSpanThisEvent error:&err]; 

} 

} 
0

至於我分析你的代碼,你應該在NSPredicate中設置日曆nil。像這樣

NSPredicate *predicateForEvents = [eventStore predicateForEventsWithStartDate:edate endDate:fdate calendars:nil]; // nil will search through all calendars. 

希望這有助於

+0

沒有豬頭...它沒有幫助.. – iSwaroop

+0

「EKEvent {\ n \ t EKEvent <0xd2be4f0> \ n {\ t title = \ t \ tLaxman Rajana \ U2019s Bir thday; \ n \ t location = \ t(null); \ n \ t calendar = \ tEKCalendar <0xd2b7b80> {title = Birthdays; type = Birthday; allowsMo​​dify = NO; color =#8295AF;}; \ n \ t警報= \ t \ t(null); \ n \ t URL = \ t \ t \ t(null); \ n \ t lastModified =(null); \ n \ t timeZone = \ t(null)\ n}; \ n \ t location = \ t(null); \ n \ t startDate = \ t2013-10-20 18:30:00; \ n \ t endDate = \ t \ t2013-10-21 18:29:59; \ n \ t allDay = \ t \ t1; \ n \ t floating = \ t1; \ n \ t recurrence = \ tEKRecurrenceRule <0x12a36f90> RRULE FREQ = YEARLY; INTERVAL = 1; \ n \ t參加者= \ t(null)\ n}; – iSwaroop