All posts

· 5 min read

How to Add Events to the Native Calendar in Capacitor

Step by step: install the Capacitor Calendar plugin, set up the iOS and Android permissions, and add events to the device calendar from your app.

Many apps deal with dates: bookings, appointments, events, deadlines. Sooner or later, users ask for a button that puts that date into their own calendar. In a Capacitor app, the web layer cannot reach the calendar of the device on its own. You need a native plugin for that.

I wrote Capacitor Calendar for exactly this job, and I still maintain it. This post introduces the plugin and takes you from installation to your first calendar event in a few minutes.

What the Plugin Does

@ebarooni/capacitor-calendar connects your Capacitor app to the native calendar. On iOS and Android, you can create, edit and list events, manage calendars and ask for calendar permissions. On iOS, it also handles reminders. On the web, it builds a downloadable .ics file instead, because a browser cannot write to a device calendar.

It is a regular Capacitor plugin, so it works with Ionic, Angular, React, Vue or any other framework that renders your UI. The README has the current table of features per platform.

Install the Plugin

Add the package and sync the native projects:

npm install @ebarooni/capacitor-calendar
npx cap sync

This installs the release for the current Capacitor version. If your app is on an older Capacitor major, the compatibility table tells you which npm tag to install.

Declare the Permissions

The calendar holds personal data, so both platforms ask the user before an app can use it. First, declare what your app needs. Only add the entries that your app really uses.

Android

Add the calendar permissions to android/app/src/main/AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

iOS

Add usage descriptions to ios/App/App/Info.plist. iOS shows this text in the permission dialog, so explain why your app needs access:

<key>NSCalendarsFullAccessUsageDescription</key>
<string>This app needs permission to read and manage your calendar events.</string>

iOS has separate keys for write-only access and for reminders. The setup section of the README lists all of them.

Request Access and Create an Event

Now the code. Ask for access first, then create the event. Dates are Unix timestamps in milliseconds.

import { CapacitorCalendar } from '@ebarooni/capacitor-calendar';

const { result } = await CapacitorCalendar.requestFullCalendarAccess();

if (result !== 'granted') {
  throw new Error('Calendar permission denied');
}

// An event that starts in one hour and lasts one hour
const startDate = Date.now() + 60 * 60 * 1000;
const endDate = startDate + 60 * 60 * 1000;

const { id } = await CapacitorCalendar.createEvent({
  title: 'Product review',
  location: 'Office',
  startDate,
  endDate,
});

Keep the returned id if you want to change or delete the event later.

Let the User Confirm in the Native Editor

Sometimes you do not want to write to the calendar directly. The user may want to pick a calendar, add an alert or change the time first. For that, open the native event editor with the event already filled in:

await CapacitorCalendar.createEventWithPrompt({
  title: 'Planning session',
  location: 'Office',
  startDate: Date.now() + 24 * 60 * 60 * 1000,
  endDate: Date.now() + 25 * 60 * 60 * 1000,
});

The user sees the familiar system screen and saves the event with one tap. On the web, the same call asks for confirmation and gives an .ics file to download.

Where to Go Next

You now have a working calendar integration. The plugin can do much more, for example list the events in a date range, manage calendars or open the calendar app. These pages always match the current release:

If you build something with the plugin, or if something does not work as you expect, open an issue on GitHub.