iOS添加日历标记事件

iOS添加日历标记事件,第1张

1、获取权限

+ (void)requestPermission:(void(^)(EKEventStore *eventStore))completion {
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    //06.07 使用 requestAccessToEntityType:completion: 方法请求使用用户的日历数据库
    if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
        // 获取访问权限
        [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            if (error)
            {
                //报错
            }
            else if (!granted)
            {
                // 被用户拒绝,不允许访问日历,提示开启权限
            }
            else
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                    // 用户既然允许事件保存到日历,那就去保存吧
                    !completion?:completion(eventStore);
                });
            }
        }];
    }
}

2、添加日历标记

// TODO: 目前做的会有重复导入同一个日程的问题,看需不需要调整
/// 写入日历
/// @param title 事件标题
/// @param location 事件地点
/// @param notes 事件备注
/// @param startDate 开始时间
/// @param endDate 结束时间
+ (void)saveDateCalendarWithTitle:(NSString *)title
                         location:(NSString *)location
                            notes:(NSString *)notes
                        startDate:(NSDate *)startDate
                          endDate:(NSDate *)endDate
                       completion:(void(^)(NSError *error))completion
{
    [self requestPermission:^(EKEventStore *eventStore) {
        // 创建事件
        EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
        
        event.title  = title;
        event.location = location;
        event.notes = notes;
        event.startDate = startDate;
        event.endDate   = endDate;
        NSString * dateString = [startDate stringWithFormat:@"yyyy-MM-dd HH:mm"];
        
        //第一次提醒  (几分钟前)
        [event addAlarm:[EKAlarm alarmWithRelativeOffset:60.0f * -5.0f]];
        
        [event setCalendar:[eventStore defaultCalendarForNewEvents]];
        NSError *err;
        [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
        !completion?:completion(err);
    }];
}

3、查询是否有相同的日历标记

/// 查询特定时间的日程
/// @param startDate 开始时间
/// @param endDate 结束时间
/// @param title  标题
+ (void)checkDateCalendarWithStartDate:(NSDate *)startDate
                               endDate:(NSDate *)endDate
                                 title:(NSString *)title
                            completion:(void(^)(NSArray *events))completion {
    [self requestPermission:^(EKEventStore *eventStore) {
        NSArray * eventArray = [eventStore calendarsForEntityType:EKEntityTypeEvent];
        NSMutableArray * onlyArray = [NSMutableArray array];
        for (int i=0; i<eventArray.count; i++) {
            EKCalendar * tempCalendar = eventArray[i];
            EKCalendarType type = tempCalendar.type;
            if (type == EKCalendarTypeLocal || type == EKCalendarTypeCalDAV) {
                [onlyArray addObject:tempCalendar];
            }
        }
        NSPredicate * predicate = [eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:eventArray];
        NSArray * events = [eventStore eventsMatchingPredicate:predicate];
        if (title && title.length != 0) {
            NSMutableArray * matchArray = [NSMutableArray array];
            for (EKEvent * event in events) {
                if ([event.title isEqualToString:title]) {
                    [matchArray addObject:event];
                }
            }
            events = [matchArray copy];
        }
        !completion?:completion(events);
    }];
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/993824.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存