IOS 系统日历开发实战

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


EventUtil.m

#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



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值