Warning: Illegal string offset 'value' in /home/content/44/9038144/html/deegeu/wp-content/themes/Divi/includes/builder/main-modules.php on line 14095

How to create events in iOS 9 and Swift 2.0

by | Oct 8, 2015




Hi, DJ Spiess here. In this tutorial, we’re going to add an event to your calendar in iOS 9 using Swift 2.0. This would normally be considered an fairly easy and common task, which it is, but with the improvements in Swift 2.0 enough has changed to warrant a new tutorial. We’re going to cover creating and deleting an event in EventKit.

The basic workflow for performing any event kit action is first get access to the event store. Then check to see if you have permission to mess with the calendar. If you don’t ask. Once you have permission, you can either create, or read and delete an event.

So first we create an event store. We can do that simply with the constructor. The event store is a context, so if you create an event object with one event store, you can’t use it with another event store somewhere else in your application. If you’re passing around events and reminders, you’ll probably want to create the event store as a singleton in your application.

Next we want to make sure we have permission to use the calendar. If we don’t check, our users might accidentally deny permission, and then will be left wondering why nothing works.

We check for access by calling authorizationStatusForEntityType() on the event store. We’ll pass the entityType as Entity. If the user has already given our application access, our test will fail. If our app doesn’t have permission, we’ll ask for it.

When we ask, we pass the entityType and a function of what to do if we get permission. If the user denies our application, the function will not be executed. Assuming we do get permission, we can perform actions on the event store.

To create a calendar event programmatically, we simply use the constructor passing it the event store. That’s why we might consider creating a singleton for the event store. If we try to create an event with one event store, then try saving that event in a different store, it won’t work. We could save it with one store, and then read it into a new event object with a different store. The gotcha is the event instance you create is tied to a specific store.

Once the event is created, we can populate the fields. We’ll set the title. That’s what gets displayed in the calendar. We’ll specify the current time for the start date, and an hour later for the end time. We can specify the calendar to add the event, but we’ll just use the default to keep this simple. Then we try to save it. We can specify a span for the event. Basically the span tells the event store we either mean this event, or this event and all others after it. We are working with just one event, so we use the constant ThisEvent. If we’re successful, we can verify the event in the calendar!

Now assume we want to delete the calendar event programmatically. We need the event identifier to load the event to delete. We’ll read it, and if it’s not nil, we’ll ask the event store to remove the event. That’s it.

I’ve created a simple program highlighting this code. I’ve created two buttons on a single view. The first button schedules an event into your calendar. The other removes the event. The code is simple, and is just to highlight how to perform the actions. Since I’m saving the event identifier after creating it, if you create a second event there will be no way to delete the first. This is just to give you an idea how to do it.

In the addEvent and removeEvent methods, we verify we have permission to use the event store. These are the IBActions for the buttons. If we have permission, we call createEvent or deleteEvent. These simply perform the action in the event store.

And that’s how you programmatically add an event to your calendar using iOS 9 and Swift 2.0.

If you liked this video, make sure you like and share. This helps get the tutorial in front of other viewers! New lessons come out each week, so make sure you subscribe. You don’t want to miss a tutorial! Thanks for watching, and I’ll see you in the next video!




Related Posts

Tools Used

  • XCode

Media Credits

All media created and owned by DJ Spiess unless listed below.

  • Background image from Pixabay.com

Get the code

The source code for “How to create events in iOS 9 and Swift 2.0” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

 git clone https://github.com/deege/deegeu-swift-eventkit.git

Go to the Support > Getting the Code page for more help.

If you find any errors in the code, feel free to let me know or issue a pull request in Git.

Don't miss another video!

New videos come out every week. Make sure you subscribe!

Comments

comments

DJ Spiess

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.

Pin It on Pinterest

Share This