Requesting Permission

suggest change

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:

Swift

import EventKit

Objective-C

#import <EventKit/EventKit.h>

Making an EKEventStore

Then, we make an EKEventStore object. This is the object from which we can access calendar and reminders data:

Swift

let eventStore = EKEventStore()

Objective-C

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.

Checking Availability

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:

Swift

switch EKEventStore.authorizationStatusForEntityType(EKEntityTypeEvent){
    case .Authorized: //...
    case .Denied: //...
    case .NotDetermined: //...
    default: break
}

Objective-C

switch ([EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent]){
    case EKAuthorizationStatus.Authorized:
        //...
        break;
    case EKAuthorizationStatus.Denied:
        //...
        break;
    case EKAuthorizationStatus.NotDetermined:
        //...
        break;
    default:
        break;
}

Requesting Permission

Put the following code in NotDetermined case:

Swift

eventStore.requestAccessToEntityType(EKEntityTypeEvent, completion: { [weak self] (userGrantedAccess, _) -> Void in
    if userGrantedAccess{
        //access calendar
    }
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


EventKit:
* Requesting Permission

Table Of Contents
12 UIView
15 UIColor
26 UIImage
28 CALayer
30 NSDate
40 iBeacon
49 NSTimer
79 NSURL
87 AWS SDK
96 NSData
101 Segues
104 EventKit
105 NSBundle
106 SiriKit
111 StoreKit
117 3D Touch
119 Keychain
122 Block
141 AirDrop
144 UISlider
145 Carthage
146 HealthKit
151 plist
157 MVVM
164 UIPhoenix
166 Simulator
168 NSArray
169 OpenGL
175 Core Data
179 MyLayout
180 UIFont
189 Security
200 Codable