问题
I am adding array of events to IOS calendar.As it has to happing on clicking a button.By doing this all the events are getting added again and again.So to over come that I used NSPredicate to get all the events and doing validation like below
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];
}
}
By the above code I am getting only birthday events not any other events which are existed. How to get all events from the calendar.
Please help me where I had gone wrong.
回答1:
I solved it by using the below code
-(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 *****
}
});
}];
}
}
Adding events to calendar by ignoring duplicates
-(void)addingDetailsToCalender
{
EKEvent *event1 = [EKEvent eventWithEventStore:eventStore];
event1.location=@"Hyderabad";
event1.title=@"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];
}
}
回答2:
As far I analysed your code you should set the calender nil in NSPredicate. Like this
NSPredicate *predicateForEvents = [eventStore predicateForEventsWithStartDate:edate endDate:fdate calendars:nil]; // nil will search through all calendars.
Hope this helps
来源:https://stackoverflow.com/questions/19496772/checking-for-duplicate-events-without-getting-all-events-in-the-event-store-in-i