Skip to content

Instantly share code, notes, and snippets.

@alex-zige
Created June 27, 2014 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-zige/ddbd70682493c6e1bf98 to your computer and use it in GitHub Desktop.
Save alex-zige/ddbd70682493c6e1bf98 to your computer and use it in GitHub Desktop.
Add calendar options
//add calendar option
- (void)_addToCalendar{
NSString *meetingTitle= [NSString stringWithFormat:@"Meeting with %@", self.meeting.participant.name.capitalizedString];
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) {return;}
BOOL matched = [self _checkForExistedCalendarInStore:store];
dispatch_async(dispatch_get_main_queue(), ^{
if (matched){
[[[UIAlertView alloc] initWithTitle:@"Warning" message:@"This meeting has already been added to your calendar." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
} else {
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = meetingTitle;
event.startDate = self.meeting.meetingDate;
event.endDate = [self.meeting.meetingDate dateByAddingTimeInterval:1*60*60];
event.endDate = self.meeting.meetingDate;
event.notes = self.meeting.notes;
[event setCalendar:[store defaultCalendarForNewEvents]];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
[[[UIAlertView alloc]initWithTitle:@"Done" message:@"You have successfully added the meeting to your calendar." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
}
});
}];
}
//private
- (BOOL)_checkForExistedCalendarInStore:(EKEventStore *)store{
NSString *meetingTitle= [NSString stringWithFormat:@"Meeting with %@", self.meeting.participant.name.capitalizedString];
NSPredicate *predicate = [store predicateForEventsWithStartDate:self.meeting.meetingDate endDate:[self.meeting.meetingDate dateByAddingTimeInterval:1*60*60] calendars:nil];
NSArray *matchedEvent = [store eventsMatchingPredicate:predicate];
for (EKEvent *event in matchedEvent) {
if([event.title isEqualToString:meetingTitle]){
return YES;
}
}
return NO;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment