Calendars and Reminders

Event Kit architecture


SimpleEKDemo is an example sample code project that utilizes events in Event Kit.


不论是 Events 还是remainder 都是存储在 calendar database中,统一用类EKEventStore检索,

EKEventStore *store = [[EKEventStore alloc] init];


ios6中,加入权限:因此要用来判断是否有权限访问EKEntityTypeEvent ,EKEntityTypeReminder,


[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {


    // handle access here


}];


Fetching events with a predicate

// Get the appropriate calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
 
// Create the start date components
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
oneDayAgoComponents.day = -1;
NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                              toDate:[NSDate date]
                                             options:0];
 
// Create the end date components
NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
oneYearFromNowComponents.year = 1;
NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents
                                                   toDate:[NSDate date]
                                                  options:0];
 
// Create the predicate from the event store's instance method
NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo
                                                        endDate:oneYearFromNow
                                                      calendars:nil];
 
// Fetch all events that match the predicate
NSArray *events = [store eventsMatchingPredicate:predicate];

Retrieving Reminders

You can call fetchRemindersMatchingPredicate:completion: to access multiple reminders that match a predicate. Pass a predicate returned by one of the following methods:

NSPredicate *predicate = [store predicateForRemindersInCalendars:nil];
 
[store fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) {
    for (EKReminder *reminder in reminders) {
        // do something for each reminder
    }
}];

 calendarItemWithIdentifier: can fetch any calendar item (reminders and events), whereas eventWithIdentifier: fetches only events.


Observing External Changes to the Calendar Database

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(storeChanged:)
                                             name:EKEventStoreChangedNotification
                                           object:eventStore];

The Event Kit UI framework provides two types of view controllers for manipulating events:

EKEventViewController *eventViewController = [[EKEventViewController alloc] init];
eventViewController.event = myEvent;
eventViewController.allowsEditing = YES;
navigationController = [[UINavigationController alloc] initWithRootViewController:eventViewController];

EKEventEditViewController* controller = [[EKEventEditViewController alloc] init];
controller.eventStore = myEventStore;
controller.editViewDelegate = self;
[self presentModalViewController:controller animated:YES];

- (void)eventEditViewController:(EKEventEditViewController *)controller 

didCompleteWithAction:(EKEventEditViewAction)action {

NSError *error = nil;

EKEvent *thisEvent = controller.event;

switch (action) {

case EKEventEditViewActionCanceled:

// Edit action canceled, do nothing.

break;

case EKEventEditViewActionSaved:

// When user hit "Done" button, save the newly created event to the event store, 

// and reload table view.

// If the new event is being added to the default calendar, then update its 

// eventsList.

if (self.defaultCalendar ==  thisEvent.calendar) {

[self.eventsList addObject:thisEvent];

}

[controller.eventStore saveEvent:controller.event span:EKSpanThisEvent error:&error];

[self.tableView reloadData];

break;

case EKEventEditViewActionDeleted:

// When deleting an event, remove the event from the event store, 

// and reload table view.

// If deleting an event from the currenly default calendar, then update its 

// eventsList.

if (self.defaultCalendar ==  thisEvent.calendar) {

[self.eventsList removeObject:thisEvent];

}

[controller.eventStore removeEvent:thisEvent span:EKSpanThisEvent error:&error];

[self.tableView reloadData];

break;

default:

break;

}

// Dismiss the modal view controller

[controller dismissModalViewControllerAnimated:YES];

}


<think>嗯,用户正在询问关于uniapp中calendars组件或API的使用方法。从对话历史看,用户之前提到了几个引用内容,涉及DayspanVuetify、React-Native-Calendar和uni-app的日历相关实现,但用户明确表示这些引用仅供参考,核心需求是了解uni-app原生的calendars组件使用。用户可能正在开发一个需要日历功能的uni-app应用,可能是小程序或H5项目。考虑到uni-app的多端兼容特性,用户可能需要一个既能在微信小程序使用,也能兼容其他平台的方案。用户没有说明具体场景,但常见需求包括:日期选择、日程展示、添加事件等。注意到用户提供的引用[3]和[4]都是关于uni-app的日历解决方案:-引用[3]展示了一个基于mx-datepicker修改的日历组件,涉及模板结构和样式定制-引用[4]提到了通过Native.js操作Android原生日历,需要权限配置但uni-app官方并没有名为"calendars"的内置组件。根据uni-app文档,最接近的是picker组件的date模式,或者使用uni-ui中的calendar扩展组件。用户可能遇到的困惑点在于:1.混淆了原生能力和插件生态2.不清楚官方方案和第三方方案的边界3.对多端适配的实现方式存在疑问回复时应该明确分层:先说明uni-app官方提供的日期选择基础能力(picker组件)再介绍更强大的社区解决方案(uni-calendar组件)最后补充原生交互场景(引用[4]的权限配置)特别要注意用户最后强调的“使用教程或文档”,意味着需要给出具体代码示例和文档链接,不能只讲概念。引用[3]的代码结构可以作为uni-calendar组件的实现参考,但要指出这是改造过的版本,官方uni-ui有更标准的实现。安全考虑:提醒用户Android权限配置只在需要调用原生日历时才用,普通日期选择不需要。如果用户只是做日期选择器,引用[4]的方案就过度了。</think> 以下是UniApp中使用日历组件的完整指南,结合官方文档和社区实践提供实用方案: --- ### 一、基础日历组件使用(官方picker) ```html <template> <view> <picker mode="date" :value="date" @change="bindDateChange"> <view>当前选择日期:{{date}}</view> </picker> </view> </template> <script> export default { data() { return { date: '2023-07-01' } }, methods: { bindDateChange(e) { this.date = e.detail.value console.log('选择日期:', this.date) } } } </script> ``` **特性说明**: 1. `mode="date"` 激活日期选择模式 2. 支持范围选择:添加 `fields="day"` 属性可选择年月日 3. 跨平台兼容:iOS/Android/H5表现一致[^1] --- ### 二、高级日历方案(推荐uni-ui扩展) 安装扩展组件: ```bash npm install @dcloudio/uni-ui ``` 页面使用: ```html <template> <uni-calendar :show="showCalendar" @close="closeCalendar" @confirm="confirmDate" :date="selectedDate" :start-date="'2020-01-01'" :end-date="'2030-12-31'"/> </template> <script> import uniCalendar from '@dcloudio/uni-ui/lib/uni-calendar/uni-calendar.vue' export default { components: { uniCalendar }, data() { return { showCalendar: true, selectedDate: '' } }, methods: { confirmDate(e) { console.log('确认日期:', e.fulldate) this.selectedDate = e.fulldate }, closeCalendar() { this.showCalendar = false } } } </script> ``` **核心功能**: 1. 月视图切换 2. 日期范围限制 3. 多日期标记(通过`insert`属性添加标记点) 4. 自定义日期样式(引用[3]样式定制方案) --- ### 三、原生日历API操作(Android/iOS) 需在`manifest.json`添加权限: ```json "permissions": { "android": { "permissions": [ "android.permission.READ_CALENDAR", "android.permission.WRITE_CALENDAR" ] }, "ios": { "permissions": { "NSCalendarsUsageDescription": "需要访问日历" } } } ``` 事件操作示例: ```javascript // 添加日历事件 const calendar = uni.requireNativePlugin('calendar') calendar.insertEvent({ title: '项目会议', location: '会议室A', startDate: Date.parse('2023-07-15 14:00'), endDate: Date.parse('2023-07-15 15:30') }, (res) => { console.log(res.eventIdentifier) }) ``` --- ### 四、最佳实践建议 1. **性能优化**:大数据量时使用分月加载(引用[2]渲染优化思路) 2. **样式定制**: ```css /* 自定义选中日期样式 */ .uni-calendar__selected { background: #4cd964 !important; } ``` 3. **跨端适配**:通过`#ifdef H5`条件编译实现平台差异化逻辑 --- **官方文档参考**: - 基础picker组件:[DCloud picker文档](https://uniapp.dcloud.net.cn/component/picker.html) - uni-calendar扩展:[uni-ui日历文档](https://ext.dcloud.net.cn/plugin?id=56) ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值