iOS开发-事件添加到系统日历提醒事项实现闹铃提醒的功能
之前开发中遇到需要将App事件添加到系统日历提醒事项,实现闹铃提醒的功能。
一、EventKit与EKEventStore
EventKit不仅能获取已存在的日程和提醒,还能在自己的app中创建、编辑、删除用户的日程和提醒,还能添加提醒、监听变化等。
EKEvent是EKCalendarItem的子类,对应日历应用中的事件。
在iOS10+中,若要访问用户日程或提醒,需要在info.plist中分别添加NSRemindersUsageDescription和NSCalendarsUsageDescription
EKEventStore类可以用来对用户的Calendar database进行查询、创建、编辑、删除等操作。我们可以使用条件来获取符合条件的一组日程,也可以用唯一标识来获取指定的一条日程。获取到的每一条日程都是一个EKEvent的实例对象,因此我们修改EKEvent对象的属性即可实现修改日程信息。
#import <EventKit/EventKit.h>
EKEventStore *store = [[EKEventStore alloc] init];
一、App事件添加到系统日历提醒事项代码
将App事件添加到系统日历提醒事项,实现闹铃提醒的功能。
由于EKEventStore对象的创建和释放会比较耗时,因此我们使用单例模式保存在app加载后只创建一个event store对象。
代码如下
SDCalendarEventManager.h
#import <Foundation/Foundation.h>
@interface SDCalendarEventManager : NSObject
+ (instancetype)sharedInstance;
/**
将App事件添加到系统日历提醒事项,实现闹铃提醒的功能
@param title 事件标题
@param location 事件位置
@param startDate 开始时间
@param endDate 结束时间
@param allDay 是否全天
@param alarmArray 闹钟集合
*/
- (void)createEventCalendarTitle:(NSString *)title location:(NSString *)location startDate:(NSDate *)startDate endDate:(NSDate *)endDate allDay:(BOOL)allDay alarmArray:(NSArray *)alarmArray;
@end
SDCalendarEventManager.m
#import "SDCalendarEventManager.h"
#import <EventKit/EventKit.h>
#import <UIKit/UIKit.h>
static SDCalendarEventManager *shareInstance = nil;
@interface SDCalendarEventManager()
@property (nonatomic, strong) EKEventStore *eventStore;
@end
@implementation SDCalendarEventManager
+ (instancetype)sharedInstance {
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
shareInstance = [[SDCalendarEventManager alloc] init];
shareInstance.eventStore = [[EKEventStore alloc] init];
});
return shareInstance;
}
/**
将App事件添加到系统日历提醒事项,实现闹铃提醒的功能
@param title 事件标题
@param location 事件位置
@param startDate 开始时间
@param endDate 结束时间
@param allDay 是否全天
@param alarmArray 闹钟集合
*/
- (void)createEventCalendarTitle:(NSString *)title location:(NSString *)location startDate:(NSDate *)startDate endDate:(NSDate *)endDate allDay:(BOOL)allDay alarmArray:(NSArray *)alarmArray{
__weak typeof(self) weakSelf = self;
if ([self.eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
if (error)
{
[strongSelf showAlert:@"添加失败,请稍后重试"];
}else if (!granted){
[strongSelf showAlert:@"不允许使用日历,请在设置中允许此App使用日历"];
}else{
EKEvent *event = [EKEvent eventWithEventStore:self.eventStore];
event.title = title;
event.location = location;
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
event.startDate = startDate;
event.endDate = endDate;
event.allDay = allDay;
//添加提醒
if (alarmArray && alarmArray.count > 0) {
for (NSString *timeString in alarmArray) {
[event addAlarm:[EKAlarm alarmWithRelativeOffset:[timeString integerValue]]];
}
}
[event setCalendar:[self.eventStore defaultCalendarForNewEvents]];
NSError *err;
[self.eventStore saveEvent:event span:EKSpanThisEvent error:&err];
[strongSelf showAlert:@"已添加到系统日历中"];
}
});
}];
}
}
- (void)showAlert:(NSString *)message {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
[alert show];
}
@end
三、小结
iOS开发-事件添加到系统日历提醒事项实现闹铃提醒的功能。EventKit与EKEventStore。
学习记录,每天不停进步。