This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo 日历(传统)

一个库,提供用于与设备的系统日历、事件、提醒和关联记录进行交互的 API。

Android
iOS
Included in Expo Go
Recommended version:
~58.0.6

expo-calendar 提供了用于与设备系统日历、事件、提醒及相关记录进行交互的 API。

此外,它提供了启动系统提供的日历界面的方法,以便用户查看或编辑事件。在安卓上,这些方法使用 Intent 启动系统日历应用。在 iOS 上,它们会以模态形式展示EKEventViewController或EKEventEditViewController。

🌐 Additionally, it provides methods to launch the system-provided calendar UI to allow user view or edit events. On Android, these methods start the system calendar app using an Intent. On iOS, they present either EKEventViewController or EKEventEditViewController as a modal.

安装

🌐 Installation

Terminal
- npx expo install expo-calendar
- yarn expo install expo-calendar
- pnpm expo install expo-calendar
- bun expo install expo-calendar

If you are installing this in an existing React Native app, make sure to install expo in your project.

应用配置中的配置

🌐 Configuration in app config

如果你在项目中使用配置插件(连续原生生成 (CNG)),可以使用其内置的 配置插件 来配置 expo-calendar。该插件允许你配置无法在运行时设置并且需要构建新的应用二进制文件才能生效的各种属性。如果你的应用不使用 CNG,那么你需要手动配置该库。

🌐 You can configure expo-calendar using its built-in config plugin if you use config plugins in your project (Continuous Native Generation (CNG)). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect. If your app does not use CNG, then you'll need to manually configure the library.

Example app.json with config plugin

app.json
{ "expo": { "plugins": [ [ "expo-calendar", { "calendarPermission": "The app needs to access your calendar." } ] ] } }

Configurable properties

NameDefaultDescription
calendarPermission"Allow $(PRODUCT_NAME) to access your calendar"
Only for: 
iOS

A string to set the NSCalendarsUsageDescription permission message.

remindersPermission"Allow $(PRODUCT_NAME) to access your reminders"
Only for: 
iOS

A string to set the NSRemindersUsageDescription permission message.

Are you using this library in an existing React Native app?

如果你没有使用连续原生生成(CNG)(而是手动使用原生 Android 和 iOS 项目),那么你需要在原生项目中配置以下权限:

🌐 If you're not using Continuous Native Generation (CNG) (you're using native android and ios projects manually), then you need to configure following permissions in your native projects:

  • 对于 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,将 NSCalendarsUsageDescription 和 NSRemindersUsageDescription 添加到你项目的 ios/[app]/Info.plist 中:

    <key>NSCalendarsUsageDescription</key> <string>Allow $(PRODUCT_NAME) to access your calendar</string> <key>NSRemindersUsageDescription</key> <string>Allow $(PRODUCT_NAME) to access your reminders</string>

用法

🌐 Usage

Basic Calendar usage
import { useEffect } from 'react'; import { StyleSheet, View, Text, Button, Platform } from 'react-native'; import * as Calendar from 'expo-calendar/legacy'; export default function App() { useEffect(() => { (async () => { const { status } = await Calendar.requestCalendarPermissionsAsync(); if (status === 'granted') { const calendars = await Calendar.getCalendarsAsync(Calendar.EntityTypes.EVENT); console.log('Here are all your calendars:'); console.log({ calendars }); } })(); }, []); return ( <View style={styles.container}> <Text>Calendar Module Example</Text> <Button title="创建新日历" onPress={createCalendar} /> </View> ); } async function getDefaultCalendarSource() { const defaultCalendar = await Calendar.getDefaultCalendarAsync(); return defaultCalendar.source; } async function createCalendar() { const defaultCalendarSource = Platform.OS === 'ios' ? await getDefaultCalendarSource() : { isLocalAccount: true, name: 'Expo Calendar' }; const newCalendarID = await Calendar.createCalendarAsync({ title: 'Expo Calendar', color: 'blue', entityType: Calendar.EntityTypes.EVENT, sourceId: defaultCalendarSource.id, source: defaultCalendarSource, name: 'internalCalendarName', ownerAccount: 'personal', accessLevel: Calendar.CalendarAccessLevel.OWNER, }); console.log(`Your new calendar ID is: ${newCalendarID}`); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'space-around', }, });

应用接口

🌐 API

import * as Calendar from 'expo-calendar/legacy';

No API data file found, sorry!

权限

🌐 Permissions

安卓

🌐 Android

如果你只打算使用系统提供的日历界面,则无需请求任何权限。

🌐 If you only intend to use the system-provided calendar UI, you don't need to request any permissions.

否则,你必须将以下权限添加到 app.json 的 expo.android.permissions 数组中。

🌐 Otherwise, you must add the following permissions to your app.json inside the expo.android.permissions array.

Android permissionDescription

READ_CALENDAR

Allows an application to read the user's calendar data.

WRITE_CALENDAR

Allows an application to write the user's calendar data.

iOS

如果你只打算使用系统提供的日历界面通过 createEventInCalendarAsync 创建事件,则无需请求权限。

🌐 If you only intend to create events using system-provided calendar UI with createEventInCalendarAsync, you don't need to request permissions.

该库使用以下用法描述键:

🌐 The following usage description keys are used by this library:

Info.plist keyDescription

NSCalendarsUsageDescription

A message that tells the user why the app is requesting access to the user’s calendar data.

NSRemindersUsageDescription

A message that tells the user why the app is requesting access to the user’s reminders.