Created
June 27, 2014 03:49
-
-
Save alex-zige/ddbd70682493c6e1bf98 to your computer and use it in GitHub Desktop.
Add calendar options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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