Search code examples
react-nativeexporeact-native-iosexpo-calendar

Adding appointment to calendar using Expo createEventAsync IOS


SDK Version: 38 Platforms(Android/iOS):

I am having difficulties getting this code to return a calendar ID, I’d appreciate help from anyone as there seems to be very little information about the new calendar API change.

async obtainCalendarPermission() {
    let permission = await Permissions.getAsync(Permissions.CALENDAR)
     if (permission.status !== 'granted') {
       permission = await Permissions.askAsync(Permissions.CALENDAR)
       return
     }
    if (permission.status !== 'granted') {
      permission = await Permissions.askAsync(Permissions.REMINDERS)
      return
     
      if (permission.status !== 'granted') {
        Alert.alert('Permission not granted to calendar')
      }
    }
    return permission
  }

async function getDefaultCalendarSource() {
  const calendars = await Calendar.getCalendarsAsync()
  const defaultCalendars = calendars.filter(
    (each) => each.source.name === 'Default',
  )
  return defaultCalendars[0].source
}

async addReservationToCalendar(date){
    await this.obtainCalendarPermission()
    const permission = await Permissions.askAsync(Permissions.REMINDERS)
    if (permission.status !== 'granted') 
    var calendars = Calendar.getCalendarsAsync().then(calendars => console.log(calendars))
      
    const defaultCalendarSource = Platform.OS === 'ios' ? await getDefaultCalendarSource(): { isLocalAccount: true, name: 'Expo Calendar' };
  console.log(defaultCalendarSource ,+'emeka')

    let dateMs = Date.parse(date)
    let startDate = new Date(dateMs)
    let endDate = new Date(dateMs + 2 * 60 * 60 * 1000)

    const calendarId = await Calendar.createEventAsync(
      defaultCalendarSource.id,
      {
        title: 'Con Fusion Table Reservation',
        startDate: startDate,
        endDate: endDate,
        timeZone: 'Asia/Hong_Kong',
        location:
          '121, Clear Water Bay Road, Clear Water Bay, Kowloon, Hong Kong',
      },
    )}

Solution

  • I have solved it. I was having a hard time getting the calendar ID as it is a requirement for the new createEventAsync() API. this is the complete code that solved the problem for me, I am pasting it here so that someone can use or improve on it.

    It generates a unique calendar ID and saves reservations to the calendar on IOS, I did not test it on android.

    async function getDefaultCalendarSource() {
      await Calendar.getCalendarsAsync().then((id) => console.log(id))
    }
    
     async obtainCalendarPermission() {
        let permission = await Permissions.getAsync(Permissions.CALENDAR)
        if (permission.status !== 'granted') {
          permission = await Permissions.askAsync(Permissions.CALENDAR)
          return
        }
        if (permission.status !== 'granted') {
          permission = await Permissions.askAsync(Permissions.REMINDERS)
          return
    
          if (permission.status !== 'granted') {
            Alert.alert('Permission not granted to calendar')
          }
        }
        return permission
      }
    async addReservationToCalendar(date) {
        await this.obtainCalendarPermission()
        var dateMs = Date.parse(date)
        var startDate = new Date(dateMs)
        var endDate = new Date(dateMs + 2 * 60 * 60 * 1000)
    
        getDefaultCalendarSource()
        const newCalendar = await Calendar.createCalendarAsync({
          title: 'Test Reservation',
          color: '#512DA8',
          entityType: Calendar.EntityTypes.EVENT,
          sourceId: getDefaultCalendarSource.id,
          source: getDefaultCalendarSource,
          name: 'Restauran Reservation',
          ownerAccount: 'personal',
          accessLevel: Calendar.CalendarAccessLevel.OWNER,
        })
    
          .then((id) => {
            Calendar.createEventAsync(id, {
              title: 'Table Reservation',
              startDate: startDate,
              endDate: endDate,
              timeZone: 'Asia/Hong_Kong',
              location:
                '121, Clear Water Bay Road, Clear Water Bay, Kowloon, Hong Kong',
            }).catch((err) => console.log(err))
            // console.log(`calendar ID is: ${id}`)
          })
          .catch((err) => console.log(err))
      }