美文网首页
在项目中增加日历提醒功能,和删除这个日历,某个时间段的删除

在项目中增加日历提醒功能,和删除这个日历,某个时间段的删除

作者: guoguojianshu | 来源:发表于2018-12-21 10:38 被阅读27次
    #import "ViewController.h"
    #import <EventKit/EventKit.h>
    #import <EventKit/EKEventStore.h>
    #import <EventKit/EKAlarm.h>
    #import "EventCalendar.h"
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
      EKAuthorizationStatus  statues = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
        //添加日历的提醒
      EKEventStore * eventStore =  [[EKEventStore alloc]init];
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
    //            同意
            }else{
    //            不同意
            }
            
        }];
    
        
        //创建事件
        EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
        
        //给事件添加标题
        event.title  = @"这里是日历事件的标题";
        //设置地点
        event.location = @"这里可以随便写地点";
        //创建测试时间,这里可以改为任意时间
        NSDate *date = [NSDate date];
        //开始时间(必须传)
        event.startDate = [date dateByAddingTimeInterval:1 * 2];
        //结束时间(必须传)
        event.endDate = [date dateByAddingTimeInterval:60 * 5 * 24];
        //全天的事件
        event.allDay = YES;
        //添加提醒
        //第一次提醒 设置事件开始之前1分钟提醒
        [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f *60*10+ 60 *6]];
        //第二次提醒 设置事件开始之前2分钟提醒
        //                [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -2.0f]];
        //第N次提醒 设置事件提醒
        //                [event addAlarm:[EKAlarm alarmWithRelativeOffset:秒]];
        //事件类容备注
        NSString * str = @"这里写备注就行了";
        event.notes = [NSString stringWithFormat:@"%@",str];
        //添加事件到日历中
        [event setCalendar:[eventStore defaultCalendarForNewEvents]];
    
        NSError *err;
        [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
        //保存事件id,方便查询和删除
        [[NSUserDefaults standardUserDefaults]setObject:event.eventIdentifier forKey:@"eventId"];
        
    //    添加提醒事项中的提醒
        [self addReminderNotify:[NSDate dateWithTimeIntervalSinceNow:0] title:@"提醒"];
    }
    
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        EKEventStore * eventStore =  [[EKEventStore alloc]init];
    
        //获取eventId
        NSString *eventId=[[NSUserDefaults standardUserDefaults]objectForKey:@"eventId"];
        if (eventId) {
            //根据eventId获取事件
            EKEvent *ekEvent=[eventStore  eventWithIdentifier:eventId];
            NSError *err;
            //删除事件 这次事件
            [eventStore removeEvent:ekEvent span:EKSpanThisEvent error:&err];
        }
    
       
        
    }
    
    -(void)getDateEvent{
        
            EKEventStore * eventStore =  [[EKEventStore alloc]init];
    
        
          NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:-3600 *24 * 90];//事件段,开始时间
        
          NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:3600 *24 * 90];//事件段,结束时间
        
          NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:nil];
        
          NSArray *events = [eventStore eventsMatchingPredicate:predicate]; // 数组里面就是时间短中的ekevent事件数组
        
            
        NSError *err;
    
            for (EKEvent *event in events) {
            
                   NSLog(@"\nidentify:%@",event.eventIdentifier);
            
            [eventStore removeEvent:event span:EKSpanThisEvent error:&err];
    
               }
        
        
    }
    
    
    
    @end
    
    -(void)addReminderNotify:(NSDate *)date title:(NSString *)title
    {
        EKEventStore *eventDB = [[EKEventStore alloc] init];
        //申请提醒权限
        [eventDB requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError * _Nullable error) {
            if (granted) {
                //创建一个提醒功能
                EKReminder *reminder = [EKReminder reminderWithEventStore:eventDB];
                //标题
                reminder.title = title;
                //添加日历
                [reminder setCalendar:[eventDB defaultCalendarForNewReminders]];
                NSCalendar *cal = [NSCalendar currentCalendar];
                [cal setTimeZone:[NSTimeZone systemTimeZone]];
                NSInteger flags = NSCalendarUnitYear | NSCalendarUnitMonth |
                NSCalendarUnitDay |NSCalendarUnitHour | NSCalendarUnitMinute |
                NSCalendarUnitSecond;
                NSDateComponents* dateComp = [cal components:flags fromDate:date];
                dateComp.timeZone = [NSTimeZone systemTimeZone];
                reminder.startDateComponents = dateComp; //开始时间
                reminder.dueDateComponents = dateComp; //到期时间
                reminder.priority = 1; //优先级
                EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date]; //添加一个车闹钟
                [reminder addAlarm:alarm];
                NSError *err;
                [eventDB saveReminder:reminder commit:YES error:&err];
                if (err) {
                    
                }
            }
        }];
    }
    

    相关文章

      网友评论

          本文标题:在项目中增加日历提醒功能,和删除这个日历,某个时间段的删除

          本文链接:https://www.haomeiwen.com/subject/ayfmkqtx.html