It seems I can't insert an event into the default calendar on my device using react-native and expo sdk. I tried this:
import * as Permissions from 'expo-permissions';
import * as Calendar from 'expo-calendar';
import { Platform } from 'react-native';
...
async obtainDefaultCalendarId() {
let calendar = null;
if (Platform.OS === 'ios') {
calendar = await Calendar.getDefaultCalendarAsync();
} else {
const calendars = await Calendar.getCalendarsAsync();
calendar = calendars
? calendars.find((cal) => cal.isPrimary) || calendars[0]
: null;
}
return calendar ? calendar.id.toString() : null;
}
async obtainCalendarPermission() {
let permission = await Permissions.getAsync(Permissions.CALENDAR);
if (permission.status !== 'granted') {
permission = await Permissions.askAsync(Permissions.CALENDAR);
if (permission.status !== 'granted') {
Alert.alert('Permission not granted to access the calendar');
}
}
return permission;
}
async addReservationToCalendar(date) {
await this.obtainCalendarPermission();
CalendarId = this.obtainDefaultCalendarId();
const startDate = new Date(Date.parse(date));
const endDate = new Date(Date.parse(date) + 2 * 60 * 60 * 1000);
Calendar.createEventAsync(CalendarId, {
title: 'Con Fusion Table Reservation',
location:
'121, Clear Water Bay Road, Clear Water Bay, Kowloon, Hong Kong',
startDate: startDate,
endDate: endDate,
timeZone: 'Asia/Hong_Kong',
});
Alert.alert('Reservation has been added to your calendar');
}
...
It asked for the permission but didn't add an event to my calendar. Before I see any error in my screen the expo client app is crushed.
I didn't get where the error occurs. When I console.log(CalendarId), I got this,
Promise {
"_40": 0,
"_55": null,
"_65": 0,
"_72": null,
}
I heartily thank if anyone helps me to figure out this.
