How to Create and Edit Calendars in Ionic and Capacitor Apps
Give your Ionic or Capacitor app a calendar of its own: list, create, rename, recolor and delete native calendars on iOS and Android with the Capacitor Calendar plugin.
In the first guide on Capacitor Calendar, we put events into the calendar that the user already has. That is enough for a single booking. But some apps own a whole stream of dates: shift plans, match days of a sports club, lectures, delivery slots. Mixing all of them into the personal calendar of the user quickly gets messy.
A better way is a calendar of your own. The user sees it as one entry in the calendar app, with its own name and color, and can hide it with one tap. Your app can rename it, change its color or remove it with all of its events. This post shows how to do all of that with Capacitor Calendar. Most Ionic apps run on Capacitor, so the same code works there too.
Before You Start
You need the plugin and the platform permissions from the first guide. To create and change calendars, your app needs full calendar access, not only write access. On iOS, that means the NSCalendarsFullAccessUsageDescription key in Info.plist. On Android, it means both READ_CALENDAR and WRITE_CALENDAR.
Calendars only exist on the device, so these calls work on iOS and Android. The web has no calendar store to write to. Check the platform before you call them:
import { Capacitor } from '@capacitor/core';
import { CapacitorCalendar } from '@ebarooni/capacitor-calendar';
async function requestCalendarAccess(): Promise<boolean> {
if (!Capacitor.isNativePlatform()) {
return false;
}
const { result } = await CapacitorCalendar.requestFullCalendarAccess();
return result === 'granted';
}See Which Calendars Exist
Start by looking at what is already there. listCalendars() returns every calendar on the device, and getDefaultCalendar() returns the one that new events go to:
const { calendars } = await CapacitorCalendar.listCalendars();
const { calendar: defaultCalendar } =
await CapacitorCalendar.getDefaultCalendar();
for (const calendar of calendars) {
console.log(calendar.id, calendar.title, calendar.color);
} Each calendar has an id, a title and a color. The allowsContentModifications flag tells you whether you can add events to it. Subscribed calendars or the birthday calendar on iOS, for example, are read-only.
Create a Calendar
Creating a calendar takes one call. The title is required, the color is optional:
const { id } = await CapacitorCalendar.createCalendar({
title: 'Team Shifts',
color: '#2E7D32',
// Android needs an account for the calendar. iOS ignores these.
accountName: 'shifts@example.com',
ownerAccount: 'shifts@example.com',
}); The call returns the id of the new calendar. Keep it, for example with the Capacitor Preferences plugin. You need it for every later step.
The two platforms store the new calendar in different places:
- Android requires
accountNameandownerAccount. The plugin creates a local calendar under that account name. It lives on the device and does not sync to a Google account, so any short name that identifies your app works. - iOS puts the calendar into iCloud when it is available, otherwise into the local store. So it can show up on the other Apple devices of the user too.
Choose the Account on iOS
On iOS, you can pick the account yourself. An account is called a source. fetchAllCalendarSources() lists them, and you pass the sourceId of the one you want:
import {
CalendarSourceType,
CapacitorCalendar,
} from '@ebarooni/capacitor-calendar';
const { result: sources } =
await CapacitorCalendar.fetchAllCalendarSources();
const local = sources.find(
(source) => source.type === CalendarSourceType.LOCAL,
);
const { id } = await CapacitorCalendar.createCalendar({
title: 'Team Shifts',
sourceId: local?.id,
}); Leave sourceId out if you have no reason to choose. The default works well for most apps.
Add Events to Your Calendar
To put an event into your own calendar, pass its id as calendarId. Without it, the event goes to the default calendar.
const startDate = new Date('2027-01-11T08:00').getTime();
const endDate = new Date('2027-01-11T16:00').getTime();
await CapacitorCalendar.createEvent({
calendarId: id,
title: 'Early shift',
location: 'Main warehouse',
startDate,
endDate,
});Rename a Calendar or Change Its Color
Plans change, and so do names. modifyCalendar() updates the title, the color or both. Fields that you leave out stay as they are:
await CapacitorCalendar.modifyCalendar({
id,
title: 'Warehouse Shifts',
color: '#1565C0',
}); For colors, use hex values like #1565C0. iOS also accepts system color names like 'indigo', but Android rejects them, so hex is the safe choice for code that runs on both.
Only change calendars that your app created. Calendars from other accounts, subscriptions or the system can be locked. iOS rejects the call for those with the message Calendar is not modifiable.
Find Your Calendar Again
The user can delete your calendar in the calendar app at any time. Then the stored id no longer works, and the plugin rejects the call with the NotFound error code. Handle that case and create the calendar again:
import {
CapacitorCalendar,
CapacitorCalendarErrorCode,
} from '@ebarooni/capacitor-calendar';
try {
await CapacitorCalendar.modifyCalendar({ id, color: '#1565C0' });
} catch (error) {
const code = (error as { code?: string }).code;
if (code === CapacitorCalendarErrorCode.NotFound) {
// The calendar is gone. Create it again and store the new id.
}
} If you lost the id, for example after a reinstall, look the calendar up by its title in listCalendars():
const { calendars } = await CapacitorCalendar.listCalendars();
const shifts = calendars.find(
(calendar) => calendar.title === 'Warehouse Shifts',
);Delete a Calendar
When the user signs out or turns the feature off, clean up after your app. deleteCalendar() removes the calendar together with all of its events:
await CapacitorCalendar.deleteCalendar({ id });This cannot be undone, and the events are gone on every device that syncs the calendar. Never call it with the id of a calendar that the user created. Ask first if the calendar may hold events that the user added by hand.
Where to Go Next
Your app now has a calendar of its own and can keep it up to date. These pages always match the current release of the plugin:
- Documentation with guides and the full API reference
- Working with calendars in the README
- Error codes and when the plugin uses them
- The source code on GitHub, where you can also ask questions and report bugs
- How to add events to the native calendar in Capacitor, the first guide on this blog
- How to migrate the Capacitor Calendar plugin to Capacitor 9, the upgrade guide for apps on version 8 of the plugin
- The Capacitor Calendar page on this site
