创建EKEvent 每日10点提现日历

2021-08-02  本文已影响0人  CodingTom
+ (void)createEKEventTask:(void (^)(void))success store:(EKEventStore *)store
{
    EKEvent *event = [EKEvent eventWithEventStore:store];
    event.title = @"签到提醒";

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *ymd = [formatter stringFromDate:[NSDate date]];

    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSDate *date = [formatter dateFromString:[NSString stringWithFormat:@"%@ 10:00:00", ymd]];

    // 提前一分钟开始
    NSDate *startDate = [NSDate dateWithTimeInterval:-60 sinceDate:date];
    // 一分钟后结束
    NSDate *endDate = [NSDate dateWithTimeInterval:60 sinceDate:date];

    event.startDate = startDate;
    event.endDate = endDate;
    event.allDay = NO;

    ///结束时间
    EKRecurrenceEnd *end = [EKRecurrenceEnd recurrenceEndWithEndDate:[NSDate dateWithTimeInterval:3600 * 24 * 31 + 3600 sinceDate:date]];

    event.recurrenceRules = @[[[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyDaily interval:1 end:end]];

    // 添加闹钟结合(开始前多少秒)若为正则是开始后多少秒。
    EKAlarm *elarm2 = [EKAlarm alarmWithRelativeOffset:60];
    [event addAlarm:elarm2];

    [event setCalendar:[store defaultCalendarForNewEvents]];

    NSError *error = nil;
    [store saveEvent:event span:EKSpanThisEvent error:&error];

    if (!error) {
        success();
    } else {
      ///错误
    }
}

上一篇下一篇

猜你喜欢

热点阅读