iOS_日历中写入日程_简单功能实现

  • 简单的实现在日历中写入日程
  • 1、添加隐私权限
  • Privacy - Calendars Usage Description
  • Privacy - Reminders Usage Description
  • 2、代码实现如下
- (void)saveCalenda{
	EKEventStore *eventStore = [[EKEventStore alloc] init];
    __weak typeof(self) weakSelf = self;
    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(), ^{
                    // 用户既然允许事件保存到日历,那就去保存吧
                    [weakSelf saveDataCalendar:eventStore];
                });
            }
        }];
    }
}
- (void)saveDataCalendar:(EKEventStore *)eventStore {
     EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
     NSDate *nowDate = [NSDate now];
    event.title  = @"我是标题" ;              //  事件标题
    event.startDate = [nowDate dateByAddingTimeInterval:0];   // 开始时间
    event.endDate   = [nowDate dateByAddingTimeInterval:60 * 5];  // 结束时间
    [event addAlarm:[EKAlarm alarmWithAbsoluteDate:nowDate]];
    [event setCalendar:[eventStore defaultCalendarForNewEvents]];
    NSError *err;
    [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
}

你可能感兴趣的:(Objective-C)