Will Zimmer Will Zimmer - 9 months ago 93
Swift Question

EventKit permission

I would like my

to request permission to the calendar when an event is attempted to be added to the calendar with access denied. It is only asking the first time the app is used and if you deny access in the settings it will not prompt the user to grant access again. Please help.

Extra Credit: I would also like it to check if the event already exists in the calendar, and if so, edit it. Any help with this as well would be appreciated!!

Here is my code:

func addToCalendar(){
let eventStore = EKEventStore()

let startDate = NSDate()
let endDate = startDate.dateByAddingTimeInterval(60 * 60) // One hour

if (EKEventStore.authorizationStatusForEntityType(.Event) != EKAuthorizationStatus.Authorized) {
eventStore.requestAccessToEntityType(.Event, completion: {
granted, error in
self.createEvent(eventStore, title: "\(self.meal.text!)", startDate: startDate, endDate: endDate)
} else {
createEvent(eventStore, title: "\(self.meal.text!)", startDate: startDate, endDate: endDate)

func createEvent(eventStore: EKEventStore, title: String, startDate: NSDate, endDate: NSDate) {
let event = EKEvent(eventStore: eventStore)

event.title = title
event.startDate = startDate
event.endDate = endDate
event.calendar = eventStore.defaultCalendarForNewEvents
event.notes = "\(self.Recipe.text!) - See Meal Planning on Listacular"

do {
try eventStore.saveEvent(event, span: .ThisEvent)
savedEventId = event.eventIdentifier
} catch {



You directly cannot prompt user to grant access again. But you can show user popup about the permission status and can request to enable the permission from device settings. Here is an example in objective-c

    EKEventStore *store = [EKEventStore new];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if (!granted)
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertController* alertController=[UIAlertController alertControllerWithTitle:@"Access to Calendar is Restricted" message:@"To re-enable, please go to Settings and turn on Calendar Settings for this app else Continue to create party without saving it to your Calendar" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"Continue" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action)
                                          [self addEvent:store]; //your method
            [alertController addAction:actionOK];
            [alertController addAction:[UIAlertAction actionWithTitle:kSETTINGS style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            [self presentViewController:alertController animated:NO completion:nil];
        [self addEvent:store]; // your method

You can check store for a specific event using this method of EKEventStore. You need to pass event identifier.

- (nullable EKEvent *)eventWithIdentifier:(NSString *)identifier;