This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 57).
Expo 日历(旧版)
一个提供 API 的库,用于与设备的系统日历、事件、提醒事项及相关记录进行交互。
CalendarAPI 的legacy版本包含在expo-calendar库中。它可以与从根路径导出的基于类的expo-calendarAPI 一起使用。要使用 legacy API,请从expo-calendar/legacy导入。
expo-calendar 提供了一个用于与设备系统日历、事件、提醒事项及相关记录交互的 API。
此外,它还提供了启动系统提供的日历 UI 的方法,允许用户查看或编辑事件。在 Android 上,这些方法会使用 Intent 启动系统日历应用。在 iOS 上,它们会以模态方式展示 EKEventViewController 或 EKEventEditViewController。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
在 app 配置中进行配置
如果你在项目中使用 config plugins(Continuous Native Generation (CNG)),你可以使用 expo-calendar 内置的 config plugin 来进行配置。该插件允许你配置一些无法在运行时设置的属性,并且这些属性需要构建新的应用二进制文件后才会生效。如果你的应用不使用 CNG,那么你需要手动配置该库。
Example app.json with config plugin
Configurable properties
Are you using this library in an existing React Native app?
如果你没有使用 Continuous Native Generation(CNG)(也就是你在手动使用原生 android 和 ios 项目),那么你需要在原生项目中配置以下权限:
-
对于 Android,请在项目的 android/app/src/main/AndroidManifest.xml 中添加
android.permission.READ_CALENDAR和android.permission.WRITE_CALENDAR权限:<uses-permission android:name="android.permission.READ_CALENDAR" /> <uses-permission android:name="android.permission.WRITE_CALENDAR" /> -
对于 iOS,请在项目的 ios/[app]/Info.plist 中添加
NSCalendarsUsageDescription和NSRemindersUsageDescription:<key>NSCalendarsUsageDescription</key> <string>允许 $(PRODUCT_NAME) 访问你的日历</string> <key>NSRemindersUsageDescription</key> <string>允许 $(PRODUCT_NAME) 访问你的提醒事项</string>
使用
API
import * as Calendar from 'expo-calendar/legacy';
启动系统提供的日历对话框
Launches the calendar UI provided by the OS to create a new event.
Promise<DialogEventResult>A promise which resolves with information about the dialog result.
Launches the calendar UI provided by the OS to edit or delete an event. On Android, this is the same as openEventInCalendarAsync.
Promise<DialogEventResult>A promise which resolves with information about the dialog result.
Sends an intent to open the specified event in the OS Calendar app.
voidLaunches the calendar UI provided by the OS to preview an event.
Promise<OpenEventDialogResult>A promise which resolves with information about the dialog result.
Hooks
Check or request permissions to access the calendar.
This uses both getCalendarPermissionsAsync and requestCalendarPermissionsAsync to interact
with the permissions.
[PermissionResponse | null, RequestPermissionMethod<PermissionResponse>, GetPermissionMethod<PermissionResponse>]Example
const [status, requestPermission] = Calendar.useCalendarPermissions();
Check or request permissions to access reminders.
This uses both getRemindersPermissionsAsync and requestRemindersPermissionsAsync to interact
with the permissions.
[PermissionResponse | null, RequestPermissionMethod<PermissionResponse>, GetPermissionMethod<PermissionResponse>]Example
const [status, requestPermission] = Calendar.useRemindersPermissions();
Methods
Creates a new attendee record and adds it to the specified event. Note that if eventId specifies
a recurring event, this will add the attendee to every instance of the event.
Promise<string>A string representing the ID of the newly created attendee record.
Creates a new calendar on the device, allowing events to be added later and displayed in the OS Calendar app.
Promise<string>A string representing the ID of the newly created calendar.
Creates a new event on the specified calendar.
Promise<string>A promise which fulfils with a string representing the ID of the newly created event.
Creates a new reminder on the specified calendar.
Promise<string>A promise which fulfils with a string representing the ID of the newly created reminder.
Deletes an existing calendar and all associated events/reminders/attendees from the device. Use with caution.
Promise<void>Gets all attendees for a given event (or instance of a recurring event).
Promise<Attendee[]>A promise which fulfils with an array of Attendee associated with the
specified event.
Checks user's permissions for accessing user's calendars.
Promise<PermissionResponse>A promise that resolves to an object of type PermissionResponse.
Gets an array of calendar objects with details about the different calendars stored on the device.
Promise<Calendar[]>An array of calendar objects matching the provided entity type (if provided).
Returns a specific event selected by ID. If a specific instance of a recurring event is desired, the start date of this instance must also be provided, as instances of recurring events do not have their own unique and stable IDs on either iOS or Android.
A promise which fulfils with an Event object matching the provided criteria, if one exists.
Returns all events in a given set of calendars over a specified time period. The filtering has
slightly different behavior per-platform - on iOS, all events that overlap at all with the
[startDate, endDate] interval are returned, whereas on Android, only events that begin on or
after the startDate and end on or before the endDate will be returned.
A promise which fulfils with an array of Event objects matching the search criteria.
Returns a list of reminders matching the provided criteria. If startDate and endDate are defined,
returns all reminders that overlap at all with the [startDate, endDate] interval - i.e. all reminders
that end after the startDate or begin before the endDate.
Promise<Reminder[]>A promise which fulfils with an array of Reminder objects matching the search criteria.
Checks user's permissions for accessing user's reminders.
Promise<PermissionResponse>A promise that resolves to an object of type PermissionResponse.
Returns whether the Calendar API is enabled on the current device. This does not check the app permissions.
Promise<boolean>Async boolean, indicating whether the Calendar API is available on the current device.
Currently, this resolves true on iOS and Android only.
Asks the user to grant permissions for accessing user's calendars.
Promise<PermissionResponse>A promise that resolves to an object of type PermissionResponse.
Promise<PermissionResponse>Asks the user to grant permissions for accessing user's reminders.
Promise<PermissionResponse>A promise that resolves to an object of type PermissionResponse.
Updates an existing attendee record. To remove a property, explicitly set it to null in details.
Promise<string>Updates the provided details of an existing calendar stored on the device. To remove a property,
explicitly set it to null in details.
Promise<string>Updates the provided details of an existing calendar stored on the device. To remove a property,
explicitly set it to null in details.
Promise<string>Updates the provided details of an existing reminder stored on the device. To remove a property,
explicitly set it to null in details.
Promise<string>Types
A person or entity that is associated with an event by being invited or fulfilling some other role.
A calendar record upon which events (or, on iOS, reminders) can be stored. Settings here apply to the calendar as a whole and how its events are displayed in the OS calendar app.
The result of presenting a calendar dialog for creating or editing an event.
An event record, or a single instance of a recurring event. On iOS, used in the Calendar app.
The result of presenting the calendar dialog for opening (viewing) an event.
Literal type: union
Permission expiration time. Currently, all permissions are granted permanently.
Acceptable values are: 'never' | number
Literal type: union
Acceptable values are: PermissionHookBehavior | Options
A recurrence rule for events or reminders, allowing the same calendar item to recur multiple times. This type is based on the iOS interface which is in turn based on the iCal RFC so you can refer to those to learn more about this potentially complex interface.
Not all the combinations make sense. For example, when frequency is DAILY, setting daysOfTheMonth makes no sense.
Options for specifying a particular instance of a recurring event. This type is used in various methods that operate on recurring events, such as updating or deleting a single occurrence or a set of future occurrences.
A source account that owns a particular calendar. Expo apps will typically not need to interact with Source objects.
Enums
Enum containing all possible user responses to the calendar UI dialogs. Depending on what dialog is presented, a subset of the values applies.
CalendarDialogResultActions.canceled = "canceled"The user canceled or dismissed the dialog.
CalendarDialogResultActions.done = "done"On Android, this is the only possible result because the OS doesn't provide enough information to determine the user's action - the user may have canceled the dialog, modified the event, or deleted it.
On iOS, this means the user simply closed the dialog.
CalendarDialogResultActions.responded = "responded"The user responded to and saved a pending event invitation.
权限
Android
如果你只打算使用系统提供的日历 UI,则不需要请求任何权限。
否则,你必须在 app.json 中的 expo.android.permissions 数组里添加以下权限。
iOS
如果你只打算使用系统提供的日历 UI 来通过 createEventInCalendarAsync 创建事件,那么你不需要请求权限。
以下使用说明键由此库使用: