随着App的开发需求,很多软件有用户发布信息的需求,该功能也许是针对用户自己的需求而发布的、也许是他人。反正就是针对事件将在未来的某个时间发生而做的预先处理。比如:参加组织某个活动,举办某个活动的等等。那么本文章就是针对作者所预先设定即将发生的事情添加到手机系统日历提醒事项,并设定需要提示的时间。
其实与大家共勉这个也是自己的项目需求,然后就...与大家共勉,不喜勿喷,多多指教。
系统日历提醒事项的几个属性:
1、事件标题
2、事件发生的位置位置
3、开始时间
4、结束时间
5、是否全天
6、事件发生前提醒的闹钟设定
项目需求:用户发布未来的时间将会发生的活动、事情。发布成功直接将此事件添加到手机系统日历提醒事项,添加成功继续后面的一系列逻辑处理...
下面直接上代码...封装的...
下面是项目抽出来的类,当然做了一些修改,保证You可以直接使用:
#import <Foundation/Foundation.h>
@interface EventCalendar : NSObject
+ (instancetype)sharedEventCalendar;
/**
* 将App事件添加到系统日历提醒事项,实现闹铃提醒的功能
*
* @param title 事件标题
* @param location 事件位置
* @param startDate 开始时间
* @param endDate 结束时间
* @param allDay 是否全天
* @param alarmArray 闹钟集合
* @param block 回调方法
*/
- (void)createEventCalendarTitle:(NSString *)title location:(NSString *)location startDate:(NSDate *)startDate endDate:(NSDate *)endDate allDay:(BOOL)allDay alarmArray:(NSArray *)alarmArray;
@end
#import "EventCalendar.h"
#import <EventKit/EventKit.h>
#import <UIKit/UIKit.h>
@implementation EventCalendar
static EventCalendar *calendar;
+ (instancetype)sharedEventCalendar{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
calendar = [[EventCalendar alloc] init];
});
return calendar;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
calendar = [super allocWithZone:zone];
});
return calendar;
}
- (void)createEventCalendarTitle:(NSString *)title location:(NSString *)location startDate:(NSDate *)startDate endDate:(NSDate *)endDate allDay:(BOOL)allDay alarmArray:(NSArray *)alarmArray{
__weak typeof(self) weakSelf = self;
EKEventStore *eventStore = [[EKEventStore alloc] init];
if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
[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: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:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
[strongSelf showAlert:@"已添加到系统日历中"];
}
});
}];
}
}
- (void)showAlert:(NSString *)message
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}
@end
等等再调用的时候,如果需要在时间发生前提醒用户,需要传入提醒时间数组,这个数组是按照秒计算的。比如时间发生前一天提醒,时间数组的元素就是:-86400
希望本文章对需要此功能的朋友有帮助,欢迎多多指教,本菜鸟将陆续分享自己遇到常用知识点的封装与大家共勉,欢迎多多指教,不喜勿喷。