Eventkit使用案例

- (void)createNewEventAndNewReminder
{
    EKEventStore *es = [[EKEventStore alloc] init];
    
    NSDate *now = [NSDate date];
    
    //事件
    [es requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (granted) {
            EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:[now dateByAddingTimeInterval:30]];//30秒
            
            EKEvent *event = [EKEvent eventWithEventStore:es];
            event.title = @"This is a new event";
            event.startDate = now;
            event.endDate = [now dateByAddingTimeInterval:30];
            [event setAllDay:YES];
            [event addAlarm:alarm];
            [event setCalendar:[es defaultCalendarForNewEvents]];
            [es saveEvent:event span:EKSpanThisEvent commit:YES error:nil];
            
            NSError *err = nil;
            if([es saveEvent:event span:EKSpanThisEvent commit:YES error:&err]){
                NSLog(@"saved!");
            }else{
                NSLog(@"%@",err);
            }
        }else{
            NSLog(@"%@",error);
        }
    }];
    
    //提醒
    [es requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
        if (granted) {
            EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:[now dateByAddingTimeInterval:30]];
            
            EKReminder *reminder = [EKReminder reminderWithEventStore:es];
            reminder.title = @"This is a reminder";
            NSCalendar *cal = [NSCalendar currentCalendar];
            [cal setTimeZone:[NSTimeZone systemTimeZone]];
            NSInteger flags = NSYearCalendarUnit | NSMonthCalendarUnit |
            NSDayCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit |
            NSSecondCalendarUnit;
            
            reminder.startDateComponents = [cal components:flags fromDate:[now dateByAddingTimeInterval:30]];//开始时间
            reminder.dueDateComponents = [cal components:flags fromDate:[now dateByAddingTimeInterval:30]]; //结束时间
            reminder.completionDate = [now dateByAddingTimeInterval:30];
            [reminder setCalendar:[es defaultCalendarForNewReminders]];
            reminder.priority = 1;//优先级
            [reminder addAlarm:alarm];
            
            NSError *err = nil;
            if([es saveReminder:reminder commit:YES error:&err]){
                NSLog(@"saved!");
            }else{
                NSLog(@"%@",err);
            }
        }else{
            NSLog(@"%@",error);
        }
    }];
}

你可能感兴趣的:(eventkit,EKEventStore,EKEvent,EKReminder)