指南
日历事件创建教程
使用 @capgo/capacitor 日历
该 @capgo/capacitor-calendar 包让一个 Capacitor 应用程序管理 iOS 和 Android 的本机日历事件。它可以请求日历权限、创建和编辑事件、打开本机日历 UI、列出日历和事件以及管理 iOS 的提醒。
安装
npm install @capgo/capacitor-calendar
npx cap sync
配置本机权限
在 iOS 上,添加您的应用程序需要的使用说明 ios/App/App/Info.plist:
<key>NSCalendarsUsageDescription</key>
<string>This app needs calendar access.</string>
<key>NSCalendarsWriteOnlyAccessUsageDescription</key>
<string>This app needs permission to add calendar events.</string>
<key>NSCalendarsFullAccessUsageDescription</key>
<string>This app needs permission to read and manage calendar events.</string>
<key>NSRemindersFullAccessUsageDescription</key>
<string>This app needs permission to read and manage reminders.</string>
在 Android 上,添加您的应用程序需要的日历权限 android/app/src/main/AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
请求权限
import { CapacitorCalendar } from '@capgo/capacitor-calendar';
const permission = await CapacitorCalendar.requestFullCalendarAccess();
if (permission.result !== 'granted') {
throw new Error('Calendar permission was not granted');
}
创建一个事件
const startDate = Date.now() + 60 * 60 * 1000;
const endDate = startDate + 60 * 60 * 1000;
const { id } = await CapacitorCalendar.createEvent({
title: 'Product review',
location: 'Capgo',
startDate,
endDate,
description: 'Created with @capgo/capacitor-calendar',
});
console.log('Created event', id);
打开本机事件 UI
await CapacitorCalendar.createEventWithPrompt({
title: 'Planning session',
location: 'Office',
startDate: Date.now() + 24 * 60 * 60 * 1000,
endDate: Date.now() + 25 * 60 * 60 * 1000,
});
列出事件
const now = Date.now();
const { result: events } = await CapacitorCalendar.listEventsInRange({
from: now,
to: now + 7 * 24 * 60 * 60 * 1000,
});
iOS 提醒
const permission = await CapacitorCalendar.requestFullRemindersAccess();
if (permission.result === 'granted') {
await CapacitorCalendar.createReminder({
title: 'Send launch notes',
dueDate: Date.now() + 2 * 24 * 60 * 60 * 1000,
});
}
Reminders仅在iOS上可用。
全参考
- GitHub: https://github.com/Cap-go/capacitor-calendar/
- 文档:/docs/plugins/calendar/