系统日历提醒事件获取与添加

针对系统日历相关提醒/事件的处理,iOS提供了两个库
EventKit:该框架提供了对用户日历数据库相关操作的API
EventKitUI:该框架提供了用于显示和编辑日历事件的视图控制器
官方的介绍可以看看,==》官方介绍

重要提示: iOS 10.0上或之后链接的iOS应用必须在其Info.plist文件中包含其需要访问的数据类型的使用说明密钥,否则将崩溃。要专门访问提醒和日历数据,它必须分别包括NSRemindersUsageDescriptionNSCalendarsUsageDescription
NSRemindersUsageDescription:App需要您的同意,才能访问提醒事项
NSCalendarsUsageDescription:App需要您的同意,才能访问日历

1.EventKit 针对日历事件/提醒的相关操作
#import <EventKit/EventKit.h> 
//以下,EventKit库中相关类
#import <EventKit/EventKitDefines.h>
#import <EventKit/EKTypes.h>
#import <EventKit/EKAlarm.h>
#import <EventKit/EKEventStore.h>
#import <EventKit/EKCalendar.h>
#import <EventKit/EKError.h>
#import <EventKit/EKEvent.h>
#import <EventKit/EKParticipant.h>
#import <EventKit/EKRecurrenceRule.h>
#import <EventKit/EKReminder.h>
#import <EventKit/EKSource.h>
#import <EventKit/EKStructuredLocation.h>

==>1.事件添加:添加的事件,在系统日历中查看

    //1.权限查看 用户需要开启权限才可操作
    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(), ^{
                
                //2-1.权限问题 无法进行添加
                if (error){
                    NSLog(@"添加失败,请稍后重试");
                }else if (!granted){
                    NSLog(@"不允许使用日历,请在设置中允许此App使用日历");
                }else{
                    //2-2.赋予权限 进行下一步添加
                    
                    //3.创建事件实例 并添加到事件存储区
                    EKEvent *event = [EKEvent eventWithEventStore:eventStore];
                    
                    //3-1.设置必要的配置
                    event.startDate = [NSDate dateWithTimeIntervalSinceNow:60];//开始时间
                    event.endDate = [NSDate dateWithTimeIntervalSinceNow:60*5];//结束时间
                    event.allDay = NO;//是否全天提醒
                    event.title = @"添加事件";//事件的标题
                    
                    //添加地理信息
                    EKStructuredLocation *structuredLocation = [EKStructuredLocation locationWithTitle:@"地理位置"];
                    structuredLocation.geoLocation = [[CLLocation alloc] initWithLatitude:120.12 longitude:30.16];
                    structuredLocation.radius = 10;
                    event.structuredLocation = structuredLocation;
                    
                    //事件所属日历
                    event.calendar = [eventStore defaultCalendarForNewEvents];
                    
                    //添加闹钟  这里最多可添加两个闹钟
                    event.alarms = @[[EKAlarm alarmWithRelativeOffset:10],[EKAlarm alarmWithRelativeOffset:15]];

                    //3-2.保存事件到事件存储区->系统日历
                    NSError *error;
                    [eventStore saveEvent:event span:EKSpanThisEvent error:&error];
                    
//                    /*
//                     这里还有一个批量处理的方法
//                     event: 事件实例
//                     span: EKSpanThisEvent->仅影响当前事件   EKSpanFutureEvents->影响这个事件及其后的一切
//                     commit: NO 暂存当前事件 但不提交到系统日历   YES 立即提交存入系统日历
//                     error: 保存处理过程中的错误
//                     */
//                    [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
//                    /*
//                     上述func  commit 设置NO时 会暂存  然后调用下面的commit才会提交到事件存储区->系统日历
//                     */
//                    NSError *commitErr;
//                    [eventStore commit:&commitErr];
                    
                }
            });
        }];
    }

==>2.添加提醒:添加的提醒,在系统提醒事项中查看

    //1.权限查看 用户需要开启权限才可操作
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]){
        [eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error){
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //2-1.权限问题 无法进行添加
                if (error){
                    NSLog(@"添加失败,请稍后重试");
                }else if (!granted){
                    NSLog(@"不允许使用日历,请在设置中允许此App使用日历");
                }else{
                    //2-2.赋予权限 进行下一步添加
                    
                    //3.创建提醒实例 并进行添加
                    EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];
                    //3-1.设置reminder 必要属性
                    reminder.title = @"今天去看牙";//提醒的标题
                    
                    
                    NSCalendar *calendar = [NSCalendar currentCalendar];
                    NSDateComponents *startComponents = [calendar componentsInTimeZone:[NSTimeZone systemTimeZone] fromDate:[NSDate dateWithTimeIntervalSinceNow:30]];
                    NSDateComponents *dueComponents = [calendar componentsInTimeZone:[NSTimeZone systemTimeZone] fromDate:[NSDate dateWithTimeIntervalSinceNow:60]];
                    startComponents.timeZone = [NSTimeZone systemTimeZone];
                    dueComponents.timeZone = [NSTimeZone systemTimeZone];
                    
                    reminder.startDateComponents = startComponents; //开始提醒的日期。
                    reminder.dueDateComponents = dueComponents;//预期完成提醒的日期
                    
                    
                    reminder.completed = NO;//设置YES将把实际完成的日期设置为当前日期。
//                    reminder.completionDate //实际完成提醒的日期
                    reminder.priority = EKReminderPriorityHigh; //优先级
                    
                    //提醒所属日历
                    reminder.calendar = [eventStore defaultCalendarForNewReminders];
                    
                    //添加闹铃
                    reminder.alarms = @[[EKAlarm alarmWithRelativeOffset:5],[EKAlarm alarmWithRelativeOffset:10]];
                    
                    //4.保存提醒实例到提醒空间
                    NSError *error;
                    [eventStore saveReminder:reminder commit:YES error:&error];
                    /*
                     设置commit NO。可以进行批量处理
                     store 调用commit  进行批量提交
                     */
//                    NSError *commitError;
//                    [eventStore commit:&commitError];
                
                    
                    NSLog(@"%@-%@",error,reminder);
                }
            });
        }];
    }

==>3.提取提醒/事件

//事件提取
    //1.权限查看 用户需要开启权限才可操作
    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(), ^{
                //2-1.权限问题 无法进行添加
                if (error){
                    NSLog(@"添加失败,请稍后重试");
                }else if (!granted){
                    NSLog(@"不允许使用日历,请在设置中允许此App使用日历");
                }else{
                    //2-2.赋予权限 进行下一步添加
                    
                    //3.提取事件
                    //这里谓词  区间最大貌似是四年  设置的时候注意 
                    //另外:提取事件的时候  只能通过这个来设置谓词
                    NSPredicate *eventPredicate = [eventStore predicateForEventsWithStartDate:[NSDate dateWithTimeInterval:-60*60*24*365*4 sinceDate:[NSDate date]] endDate:[NSDate date] calendars:@[[eventStore defaultCalendarForNewEvents]]];
                    NSLog(@"%@",eventPredicate);
                    [eventStore enumerateEventsMatchingPredicate:eventPredicate usingBlock:^(EKEvent * _Nonnull event, BOOL * _Nonnull stop) {
                        NSLog(@"%@",event);
                    }];
                }
            });
        }];
    }


//提醒提取
//1.权限查看 用户需要开启权限才可操作
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]){
        [eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error){
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //2-1.权限问题 无法进行添加
                if (error){
                    NSLog(@"添加失败,请稍后重试");
                }else if (!granted){
                    NSLog(@"不允许使用日历,请在设置中允许此App使用日历");
                }else{
                    //2-2.赋予权限 进行下一步添加
                    
                    //3.提取提醒
                    NSPredicate *predicate = [eventStore predicateForRemindersInCalendars:@[[eventStore defaultCalendarForNewReminders]]];
                    [eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray<EKReminder *> * _Nullable reminders) {
                        [reminders enumerateObjectsUsingBlock:^(EKReminder * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                            NSLog(@"%@-%@",obj.title,obj);
                        }];
                    }];

                }
            });
        }];
    }

==>4.几个主要类
==>4-1 EKEventStore 访问用户日历和提醒事件并支持新事件计划的对象。
==>4-2 EKEvent 事件实例(设置相关信息 地理位置 闹钟提醒等)
==>4-3 EKReminder 提醒实例(设置相关信息 闹钟提醒等 这里地理信息没法设置 但是在设置闹钟的实例中能进行相关的设置)
==>4-4 EKAlarm 闹钟实例(先关设置 地理信息等)
==>4-5 EKRecurrenceRule 设置重复规则(日 周 月 年)
==>4-6 EKStructuredLocation 配置地理信息

2. EventKitUI UI界面搭建
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,185评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,652评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,524评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,339评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,387评论 6 391
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,287评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,130评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,985评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,420评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,617评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,779评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,477评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,088评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,716评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,857评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,876评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,700评论 2 354

推荐阅读更多精彩内容

  • #import <EventKit/EventKit.h>- (void)saveEvent:(id)sender...
    夭Y夭阅读 5,314评论 1 4
  • 2017年11月4日。去的黄龙沃尔玛下面的店。 点的麻辣罐罐米线,挺有滋味的,可以免费加一份米线,很多,不加米线我...
    天使之城悦阅读 272评论 0 0
  • 我一老叟入简书, 为圆梦想步其途。 写有文字三十万, 故事占去多篇幅。 题材多为身边出, 加工提炼百态浮。 他人褒...
    风云翁阅读 2,733评论 22 150
  • 关于爱情 从来没有一颗 撞大运的心 只愿 遇到的人 刚好在寻找自己 波澜不惊 静静地 听彼此的心 把世界 停留在彼...
    ClaireBear531阅读 118评论 0 1