系统日历添加日程

import EventKit
lazy var eventStore = EKEventStore()



    /// 系统日历添加提醒事件
    /// - Parameter eventTitle: event 名字
    /// - Parameter eventNotes: event 信息
    /// - Parameter eventRepeatTime: event 重复提醒时间   Date 类型 精确到分钟
    /// - Parameter eventEndTime: event 结束时间 Date 类型
    /// - Parameter eventRepeatDate: event 重复日 例: 0, 1, 2   0 为周日 以此类推
    /// - Parameter eventAlarmTime: event 提醒时间 单位: min 为空 则不提醒
    /// - Parameter eventIdentifier: event Block 系统分配事件 identifier
    /// - Parameter eventAddFailure: event Block 添加失败 一般会是 权限问题
    func addACalendarTime(_ eventTitle: String,
                          eventNotes: String,
                          eventRepeatTime: Date,
                          eventEndTime: Date,
                          eventRepeatDate: String?,
                          eventAlarmTime: Int?,
                          eventIdentifier: @escaping ((_ identifier: String) -> Void),
                          eventAddFailure: @escaping (() -> Void)) {
        let event = EKEvent(eventStore: self.eventStore)
        
        event.title = eventTitle
        event.notes = eventNotes
        event.startDate = eventRepeatTime
        event.endDate   = eventRepeatTime
        event.isAllDay = false
        if let time = eventAlarmTime {
            event.addAlarm(EKAlarm(relativeOffset: TimeInterval(-60 * time)))
        }
        event.calendar = self.eventStore.defaultCalendarForNewEvents
        
        var weekArr: [EKRecurrenceDayOfWeek] = []
        
        if let wt = eventRepeatDate {
            let wArr = wt.components(separatedBy: ",")
            for index in 0.. -1 {
                    let daysOfWeek = EKRecurrenceDayOfWeek(EKWeekday.init(rawValue: weekInt + 1)!)
                    weekArr.append(daysOfWeek)
                }
            }
        }
        
        let rule = EKRecurrenceRule(recurrenceWith: .weekly,
                                    interval: 1,
                                    daysOfTheWeek: weekArr,
                                    daysOfTheMonth: nil,
                                    monthsOfTheYear: nil,
                                    weeksOfTheYear: nil,
                                    daysOfTheYear: nil,
                                    setPositions: nil,
                                    end: EKRecurrenceEnd(end: eventEndTime))
        
        event.addRecurrenceRule(rule)
        
        do {
            try self.eventStore.save(event, span: .thisEvent)
            eventIdentifier(event.eventIdentifier)
            PLLog("添加成功")
        }catch {
            PLLog("添加失败")
        }
    }

你可能感兴趣的:(系统日历添加日程)