EventUtil.h
#import <Foundation/Foundation.h>
@interface EventUtil : NSObject
+ (instancetype)shareInstance;
//根据课程存入日历
- (void)saveEventByCourse:(Course_M *)course_M block:(void(^)(BOOL isSuccesed))block;
//根据课程id存入日历
- (void)saveEventByCourseWithId:(NSString *)course_id WithName:(NSString *)course_name block:(void(^)(BOOL isSuccesed))block;
//根据课程删除日历
- (void)deleteEventByCourse:(Course_M *)course_M block:(void(^)(BOOL isSuccesed))block;
//根据课程id删除日历
- (void)deleteEventByCourseWithId:(NSString *)course_id block:(void(^)(BOOL isSuccesed))block;
//查看课程是否在日历中存在
- (void)isEventByBourse:(Course_M *)course_M block:(void(^)(BOOL isExsit))block;
//根据id查看课程是否在日历中存在
- (void)isEventByBourseWithId:(NSString *)course_id block:(void(^)(BOOL isExsit))block;
@end
#import "EventUtil.h"
#import <EventKit/EventKit.h>
@interface EventUtil()
@property (nonatomic, strong) EKEventStore *shareStore;
@end
@implementation EventUtil
static EventUtil *_shareInstance = nil;
+ (instancetype)shareInstance;
{
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
_shareInstance = [[EventUtil alloc] init];
});
return _shareInstance;
}
//根据课程存入日历
- (void)saveEventByCourse:(Course_M *)course_M block:(void(^)(BOOL isSuccesed))block
{
[self saveEventByCourseWithId:course_M.course_id WithName:course_M.course_name block:block];
}
- (void)saveEventByCourseWithId:(NSString *)course_id WithName:(NSString *)course_name block:(void(^)(BOOL isSuccesed))block
{
[self shareStore];
// the selector is available, so we must be on iOS 6 or newer
[_shareStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
//错误细心
// display error message here
block(NO);
}
else if (!granted)
{
//被用户拒绝,不允许访问日历
// display access denied error message here
UIAlertView *alertView = [UIAlertView bk_alertViewWithTitle:@"用户被拒绝访问日历" message:@"请在设置中修改APP访问权限"];
[alertView bk_addButtonWithTitle:@"确定" handler:^{
}];
[alertView show];
block(NO);
}
else
{
// access granted
// ***** do the important stuff here *****
//事件保存到日历
//创建事件
EKEvent *event = [EKEvent eventWithEventStore:_shareStore];
event.title = course_name;
event.location = @"";
event.notes = @"点击URL调转到相应课程界面";
event.URL = [NSURL URLWithString:[NSString stringWithFormat:@""]];
NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
[tempFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
event.startDate = [[NSDate alloc] init];
event.endDate = [[NSDate alloc] init];
event.allDay = NO;
//添加提醒
[event addAlarm:[EKAlarm alarmWithRelativeOffset:30]];
[event addAlarm:[EKAlarm alarmWithRelativeOffset:60]];
[event setCalendar:[_shareStore defaultCalendarForNewEvents]];
NSError *err;
[_shareStore saveEvent:event span:EKSpanThisEvent error:&err];
[[NSUserDefaults standardUserDefaults] setObject:event.eventIdentifier forKey:[NSString stringWithFormat:@"%@_%@",[MCLoginModel shareInstance].userModel.userId,course_id]];
DLog(@"event id = %@",event.eventIdentifier);
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Event Created"
message:@"Yay!?"
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alert show];
NSLog(@"保存成功");
block(YES);
}
});
}];
}
//根据课程删除日历
- (void)deleteEventByCourse:(Course_M *)course_M block:(void(^)(BOOL isSuccesed))block
{
[self deleteEventByCourseWithId:course_M.course_id block:block];
}
- (void)deleteEventByCourseWithId:(NSString *)course_id block:(void(^)(BOOL isSuccesed))block
{
[self shareStore];
// the selector is available, so we must be on iOS 6 or newer
[_shareStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
//错误细心
// display error message here
block(NO);
}
else if (!granted)
{
//被用户拒绝,不允许访问日历
// display access denied error message here
UIAlertView *alertView = [UIAlertView bk_alertViewWithTitle:@"用户被拒绝访问日历" message:@"请在设置中修改APP访问权限"];
[alertView bk_addButtonWithTitle:@"确定" handler:^{
}];
[alertView show];
block(NO);
}
else
{
// access granted
// ***** do the important stuff here *****
//事件保存到日历
//创建事件
NSString *eventID = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@_%@",[MCLoginModel shareInstance].userModel.userId,course_id]];
EKEvent *event = [_shareStore eventWithIdentifier:eventID];
if(event)
{
NSError *err;
[_shareStore removeEvent:event span:EKSpanThisEvent error:&err];
NSLog(@"删除成功");
block(YES);
}else
{
block(NO);
}
}
});
}];
}
//查看课程是否在日历中存在
- (void)isEventByBourse:(Course_M *)course_M block:(void(^)(BOOL isExsit))block
{
[self isEventByBourseWithId:course_M.course_id block:block];
}
- (void)isEventByBourseWithId:(NSString *)course_id block:(void(^)(BOOL isExsit))block
{
[self shareStore];
__block BOOL isExit;
// the selector is available, so we must be on iOS 6 or newer
[_shareStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error)
{
//错误细心
// display error message here
block(NO);
}
else if (!granted)
{
//被用户拒绝,不允许访问日历
// display access denied error message here
UIAlertView *alertView = [UIAlertView bk_alertViewWithTitle:@"用户被拒绝访问日历" message:@"请在设置中修改APP访问权限"];
[alertView bk_addButtonWithTitle:@"确定" handler:^{
}];
[alertView show];
block(NO);
}
else
{
// access granted
// ***** do the important stuff here *****
//事件保存到日历
//创建事件
NSString *eventID = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@_%@",[MCLoginModel shareInstance].userModel.userId,course_id]];
EKEvent *event = [_shareStore eventWithIdentifier:eventID];
if(event)
{
isExit = YES;
block(YES);
}else
{
block(NO);
}
}
});
}];
}
#pragma mark
#pragma mark init
- (EKEventStore *)shareStore
{
if(!_shareStore)
{
_shareStore = [[EKEventStore alloc] init];
}
return _shareStore;
}
@end