I have a button in my app that brings up the EventEditViewControllerWithEventStore view. When The user presses done, the event is added to the calendar and I present a notification when it's added. However, how can I tell if the user canceled? As of now the notification is presented regardless if the user pressed done or cancel.
This is what I have so far:
- (IBAction)addEvent:(id)sender {
EKEventStore *eventStore = [[EKEventStore alloc] init];
if([eventStore respondsToSelector: @selector(requestAccessToEntityType:completion:)]) {
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL permissionGranted, NSError *error) {
if (permissionGranted){
NSLog(@"PERMISSION GRANTED.");
[self performSelectorOnMainThread:@selector(presentEventEditViewControllerWithEventStore:) withObject:eventStore waitUntilDone:NO];
}
else {
NSLog(@"NO PERMISSION.");
[TSMessage showNotificationInViewController:self withTitle:@"Oops! Permission Required" withMessage:@"In order to use this feature, please enable calendar access for this app under settings." withType:TSMessageNotificationTypeWarning withDuration:TSMessageNotificationDurationAutomatic withCallback:nil atPosition:TSMessageNotificationPositionTop];
}
}];
}
else {
// If device is > 6.0
[self presentEventEditViewControllerWithEventStore:eventStore];
}
}
- (void)presentEventEditViewControllerWithEventStore:(EKEventStore*)eventStore
{
EKEventEditViewController *vc = [[EKEventEditViewController alloc] init];
vc.eventStore = eventStore;
EKEvent* event = [EKEvent eventWithEventStore:eventStore];
//Pre-populated Info
event.title = @"Event title";
event.startDate = [NSDate date];
event.endDate = [NSDate date];
event.URL = [NSURL URLWithString:@"http://example.com"];
event.notes = @"Event Notes Can Go Here.";
event.allDay = YES;
event.location = @"Earth";
event.availability = EKEventAvailabilityTentative;
vc.event = event;
vc.editViewDelegate = self;
[self presentViewController:vc animated:YES completion:nil];
}
#pragma EKEventEditViewDelegate
- (void)eventEditViewController:(EKEventEditViewController*)controller
didCompleteWithAction:(EKEventEditViewAction)action {
[controller dismissViewControllerAnimated:YES completion:nil];
[self performSelector:@selector(eventAddedSuccessfully) withObject:nil afterDelay:0.5];
}
#pragma TSMessages
- (void)eventAddedSuccessfully {
NSLog(@"Event was successfully added.");
[TSMessage showNotificationInViewController:self withTitle:@"Success!" withMessage:@"The event was added to your calendar." withType:TSMessageNotificationTypeSuccess withDuration:TSMessageNotificationDurationAutomatic withCallback:nil atPosition:TSMessageNotificationPositionTop];
}
Set the delegate
method of your EKEventEditViewController
to self
and then, add the following method to catch it.
#pragma mark -
#pragma mark EKEventEditViewDelegate
// Overriding EKEventEditViewDelegate method to update event store according to user actions.
- (void)eventEditViewController:(EKEventEditViewController *)controller
didCompleteWithAction:(EKEventEditViewAction)action {
NSError *error = nil;
//EKEvent *thisEvent = controller.event;
switch (action) {
case EKEventEditViewActionCanceled:
// Edit action canceled, do nothing.
break;
case EKEventEditViewActionSaved:
// When user hit "Done" button, save the newly created event to the event store,
// and reload table view.
// If the new event is being added to the default calendar, then update its
// eventsList.
[controller.eventStore saveEvent:controller.event span:EKSpanThisEvent error:&error];
break;
case EKEventEditViewActionDeleted:
// When deleting an event, remove the event from the event store,
// and reload table view.
// If deleting an event from the currenly default calendar, then update its
// eventsList.
break;
default:
break;
}
// Dismiss the modal view controller
[controller dismissViewControllerAnimated:YES completion:nil];
}
Also, make sure your your interface in header file in conforming to the EKEventEditViewDelegate like below:
.h file
@interface EventViewController : UIViewController <EKEventEditViewDelegate> {}
Here is how to check to see if you were granted permission to add/modify events:
EKEventStore *store = [[EKEventStore alloc] init];
if([store respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
//Access not granted-------------
if(!granted){
}
//Access granted
}else{
}
}];
}