vonovak / react-native-add-calendar-event

Create, view or edit events in react native using the standard iOS / Android dialogs
MIT License
344 stars 103 forks source link

[Android] Calendar read permission (android.permission.READ_CALENDAR) is required #185

Closed johannessachse closed 7 months ago

johannessachse commented 8 months ago

Reproduce:

  1. Ask for calendar write permission
  2. Add an event to the calendar
  3. Click on save
  4. The app crashes:
    Permission Denial: reading com.android.providers.calendar.CalendarProvider2 uri content://com.android.calendar/events from pid=21306, uid=10063 requires android.permission.READ_CALENDAR, or grantUriPermission()
  5. The user needs to restart the app

Possible solutions:

  1. Fix add calendar event handling so that no calendar read permission is required anymore OR
  2. Additionally ask for calendar read permission
vonovak commented 7 months ago

Hello and thanks for asking, this module does not handle permissions, as documented in the readme: https://github.com/vonovak/react-native-add-calendar-event?tab=readme-ov-file#permissions Thank you 🙂

moltedopablo commented 2 months ago

Hi, in my case (working on android) the calendar opened but when going back it would crash. This issue was the key, the read permission was necessary for this to work. I'm writing this for future reference and I'd change the example code to something like this:

  const addCalendarEvent = () => {
      Permissions.requestMultiple(
        Platform.select({
          ios: [Permissions.PERMISSIONS.IOS.CALENDARS_WRITE_ONLY],
          android: [
            Permissions.PERMISSIONS.ANDROID.WRITE_CALENDAR,
            Permissions.PERMISSIONS.ANDROID.READ_CALENDAR,
          ],
        })
      )
      .then((result) => {
        if (!allPermssionGranted(result)) {
          throw new Error(`No permission: ${JSON.stringify(result)}`)
        }
        return AddCalendarEvent.presentEventCreatingDialog(eventConfig)
      })
      .then((eventInfo) => {
        // handle success - receives an object with `calendarItemIdentifier` and `eventIdentifier` keys, both of type string.
        // These are two different identifiers on iOS.
        // On Android, where they are both equal and represent the event id, also strings.
        // when { action: 'CANCELED' } is returned, the dialog was dismissed
        console.warn(JSON.stringify(eventInfo))
      })
      .catch((error) => {
        // handle error such as when user rejected permissions
        console.warn(error)
      })
  }
  const allPermssionGranted = (result) => {
    return Object.values(result).every((permission) => permission ===  Permissions.RESULTS.GRANTED);
  }