iOS访问系统日历 添加提醒事件

1 添加隐私请求提示

Privacy - Calendars Usage Description

2 代码
#import

// 添加提醒事件
- (void)addEventWithTimeStr:(NSString *)timeStr title:(NSString *)title planId:(NSString *)planId
{
    
    EKEventStore *store = [[EKEventStore alloc] init];

    if ([store respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
        
        [store requestAccessToEntityType:(EKEntityTypeEvent) completion:^(BOOL granted, NSError * _Nullable error) {
           
            dispatch_async(dispatch_get_main_queue(), ^{

                
                if (error) {
                    NSLog(@"发生错误");
                } else if (!granted) {
                    NSLog(@"未获得使用日历权限");
                } else {
                    EKEvent *event = [EKEvent eventWithEventStore:store];
                    event.title = title;
//                    event.location = @"位置";
                    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                    NSDate *date = [formatter dateFromString:timeStr];
                    // 提前一个小时开始
                    NSDate *startDate = [NSDate dateWithTimeInterval:-3600 sinceDate:date];
                    // 提前一分钟结束
                    NSDate *endDate = [NSDate dateWithTimeInterval:60 sinceDate:date];
                    event.startDate = startDate;
                    event.endDate = endDate;
                    event.allDay = NO;
                    // 添加闹钟结合(开始前多少秒)若为正则是开始后多少秒。
                    EKAlarm *elarm2 = [EKAlarm alarmWithRelativeOffset:-20];
                    [event addAlarm:elarm2];
                    EKAlarm *elarm = [EKAlarm alarmWithRelativeOffset:-10];
                    [event addAlarm:elarm];
                    [event setCalendar:[store defaultCalendarForNewEvents]];
                    
                    NSError *error = nil;
                    [store saveEvent:event span:EKSpanThisEvent error:&error];
                    if (!error) {
                        NSLog(@"添加时间成功");
                        //添加成功后需要保存日历关键字
                        NSString *iden = event.eventIdentifier;
                        // 保存在沙盒,避免重复添加等其他判断
                        [[NSUserDefaults standardUserDefaults] setObject:iden forKey:planId];
                        [[NSUserDefaults standardUserDefaults] synchronize];
                    }
                }
            });
        }];
    }
    
}

// 移除提醒事件
- (void)removeEventWithPlanID:(NSString *)planId
{
    
    
    EKEventStore *store = [[EKEventStore alloc] init];
    // 获取上面的这个ID呀。
    NSString *identifier = [NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:planId]];
    EKEvent *event = [store eventWithIdentifier:identifier];
    __block BOOL isRemoved = NO;
    dispatch_async(dispatch_get_main_queue(), ^{
        
        NSError *err = nil;
        
        isRemoved = [store removeEvent:event span:EKSpanThisEvent commit:YES error:&err];
        
        if (!err) {
            NSLog(@"删除日历成功");
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:identifier];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
        
    });
    
    
}
在这里插入代码片

你可能感兴趣的:(iOS开发笔记)