Think And Build

Tips&tricks

Calendar Events and Permissions in iOS 6

Posted on .

Calendar Events and Permissions in iOS 6

Introduction

Prior to iOS 6, accessing the calendar didn’t require any permission. We just had to create an event store and then we were able to add events through it.

With iOS6 the story is slightly different: to create a new event we ask the user for the permission to access his calendar.

Thanks to the new function requestAccessToEntityType:completion: that has been added to the EKEventStore, this operation is extremely simple. Let’s see how to deal with it with a simple example (you can find the source code at the end of this post).

The application I created for this post adds an event to the user’s calendar at the current date, but before doing that, it asks the user for the right permissions. Check this portion of code to understand how that function works:


//Request the access to the Calendar
[eventStore requestAccessToEntityType:EKEntityTypeEvent

 completion:^(BOOL granted,NSError* error){
           
            //Access not granted-------------
            if(!granted){

            }
           
            //Access granted------------------
            }else{

						}
 }];

Briefly, we request the permission to access a specific entity type (this case EKEventTypeEvent). The user will be warned by a system alert which asks him whether the application can access his calendar or not. Depending on the user’s answer we define two different behaviours within the completion block.

This block is executed every time we ask for this permission but the system alert is prompted to the user only once. To change his choice the user has to access device privacy settings.

Note: If you delete the application from your device, the privacy settings will not be removed and will be there even if you reinstall the application. To remove them you have to manually reset the privacy settings from General-> Reset-> Reset Location & Privacy.

Yari D'areglia

Yari D'areglia

https://www.thinkandbuild.it

Senior iOS developer @ Neato Robotics by day, game developer and wannabe artist @ Black Robot Games by night.

Navigation