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。
重要 为了提供更快速的更新,
expo-calendar目前在 Expo Go 和 Snack 中不受支持。要使用它,请创建一个 开发构建。
expo-calendar 提供了用于与设备系统日历、事件、提醒及相关记录进行交互的 API。
此外,它提供了方法来启动系统提供的日历界面,以允许用户查看或编辑事件。在 iOS 上,它们以模态方式显示 EKEventViewController 或 EKEventEditViewController。
🌐 Additionally, it provides methods to launch the system-provided calendar UI to allow the user to view or edit events. On iOS, they present either EKEventViewController or EKEventEditViewController as a modal.
安装
🌐 Installation
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
Configurable properties
要本地化 iOS 日历或提醒权限提示消息,请在每个本地化文件的 ios 对象中添加对应的使用说明键。calendarPermission 属性会同时设置 NSCalendarsUsageDescription 和 NSCalendarsFullAccessUsageDescription,而 remindersPermission 会同时设置 NSRemindersUsageDescription 和 NSRemindersFullAccessUsageDescription。Expo 会在预构建期间将本地化的值写入 InfoPlist.strings。
🌐 To localize an iOS calendar or reminders permission message, add the matching usage description keys to the ios object in each locale file. The calendarPermission property sets both NSCalendarsUsageDescription and NSCalendarsFullAccessUsageDescription, and remindersPermission sets both NSRemindersUsageDescription and NSRemindersFullAccessUsageDescription. Expo writes the localized values to InfoPlist.strings during prebuild.
Are you using this library in an existing React Native app?
如果你没有使用连续原生生成(CNG)(而是手动使用原生 iOS 项目),那么你需要在你的原生项目中配置以下权限:
🌐 If you're not using Continuous Native Generation (CNG) (you're using native ios project manually), then you need to configure following permissions in your native project:
-
对于 iOS,将
NSCalendarsUsageDescription、NSCalendarsFullAccessUsageDescription和NSRemindersUsageDescription添加到你项目的 ios/[app]/Info.plist 中:<key>NSCalendarsUsageDescription</key> <string>Allow $(PRODUCT_NAME) to access your calendar</string> <key>NSCalendarsFullAccessUsageDescription</key> <string>Allow $(PRODUCT_NAME) to access your calendar</string> <key>NSRemindersUsageDescription</key> <string>Allow $(PRODUCT_NAME) to access your reminders</string>在 iOS 17 及以上版本请求仅写日历访问权限时,添加
NSCalendarsWriteOnlyAccessUsageDescription而不是NSCalendarsFullAccessUsageDescription:<key>NSCalendarsWriteOnlyAccessUsageDescription</key> <string>Allow $(PRODUCT_NAME) to add events to your calendars</string>
用法
🌐 Usage
import * as Calendar from 'expo-calendar'; import { useEffect } from 'react'; import { StyleSheet, View, Text, Button } from 'react-native'; const BasicUsage = () => { useEffect(() => { (async () => { const { status } = await Calendar.requestCalendarPermissions(); if (status === 'granted') { const calendars = Calendar.getCalendars(Calendar.EntityTypes.EVENT); console.log('Here are all your calendars:'); console.log(JSON.stringify(calendars)); } })(); }, []); return ( <View style={styles.container}> <Text>Calendar Module Example</Text> <Button title="创建新日历" onPress={createCalendar} /> </View> ); }; async function createCalendar() { const newCalendar = await Calendar.createCalendar({ title: 'Expo Calendar', color: 'blue', entityType: Calendar.EntityTypes.EVENT, }); console.log(`Your new calendar: ${JSON.stringify(newCalendar)}`); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'space-around', }, });
应用接口
🌐 API
import * as Calendar from 'expo-calendar';
除非另有说明,所有日期均以 ISO 8601 格式返回。
🌐 Unless specified otherwise, all dates are returned in the ISO 8601 format.
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.
iOS
如果只想添加事件而不读取现有日历数据,可以在 config 插件 中启用 writeOnlyAccess 选项,并通过向 requestCalendarPermissions 传递 true 来请求仅写权限。这对于像 ExpoCalendar.createEvent 这样的方法就足够了。像 getCalendars、listEvents 或 presentPicker 这样需要读取日历数据的方法,则需要完整的日历访问权限。
🌐 To only add events without reading existing calendar data, enable the writeOnlyAccess option in the config plugin and request write-only access by passing true to requestCalendarPermissions. This is sufficient for methods like ExpoCalendar.createEvent. Methods that read calendar data, such as getCalendars, listEvents, or presentPicker, require full calendar access.
该库使用以下用法描述键:
🌐 The following usage description keys are used by this library: