Your app can't access your reminders and your calendar without permission. Instead, it must show an alert to user, requesting him/her to grant access to events for the app.
To get started, import the EventKit
framework:
import EventKit
#import <EventKit/EventKit.h>
EKEventStore
Then, we make an EKEventStore
object. This is the object from which we can access calendar and reminders data:
let eventStore = EKEventStore()
EKEventStore *eventStore = [[EKEventStore alloc] init];
Note
Making an
EKEventStore
object every time we need to access calendar is not efficient. Try to make it once and use it everywhere in your code.
Availability has three different status: Authorized, Denied and Not Determined. Not Determined means the app needs to grant access.
To check availability, we use authorizationStatusForEntityType()
method of the EKEventStore
object:
switch EKEventStore.authorizationStatusForEntityType(EKEntityTypeEvent){
case .Authorized: //...
case .Denied: //...
case .NotDetermined: //...
default: break
}
switch ([EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent]){
case EKAuthorizationStatus.Authorized:
//...
break;
case EKAuthorizationStatus.Denied:
//...
break;
case EKAuthorizationStatus.NotDetermined:
//...
break;
default:
break;
}
Put the following code in NotDetermined
case:
eventStore.requestAccessToEntityType(EKEntityTypeEvent, completion: { [weak self] (userGrantedAccess, _) -> Void in
if userGrantedAccess{
//access calendar
}
}