JKSwiftExtension宣肚,測試用例在 JKEKEventViewController 里面
目錄:
- 1暖呕、日歷基本的使用
- 根據(jù)時(shí)間段獲取日歷事件
- 添加日歷事件
- 修改日歷事件
- 刪除日歷事件
- 2、提醒事件的基本的使用
- 查詢出所有提醒事件
- 添加提醒事件
- 修改提醒事件
- 移除提醒事件
一评抚、日歷基本的使用
import EventKit
public class JKEKEvent: NSObject {}
// MARK:- 一、日歷基本的使用
public extension JKEKEvent {
// MARK: 1.1、根據(jù)時(shí)間段獲取日歷事件
/// 根據(jù)時(shí)間段獲取日歷事件
/// - Parameters:
/// - startDate: 開始時(shí)間
/// - endDate: 結(jié)束時(shí)間
/// - eventsClosure: 事件閉包
static func selectCalendarsEvents(startDate: Date, endDate: Date, eventsClosure: @escaping (([EKEvent]) -> Void)) {
let eventStore = EKEventStore()
// 請求日歷事件
eventStore.requestAccess(to: .event, completion: {
granted, error in
if (granted) && (error == nil) {
// 獲取本地日歷(剔除節(jié)假日纳寂,生日等其他系統(tǒng)日歷)
let calendars = eventStore.calendars(for: .event).filter({
(calender) -> Bool in
return calender.type == .local || calender.type == .calDAV
})
let predicate = eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: calendars)
let eV = eventStore.events(matching: predicate)
// 重新刷新表格數(shù)據(jù)
DispatchQueue.main.async {
eventsClosure(eV)
}
} else {
DispatchQueue.main.async {
eventsClosure([])
}
}
})
}
// MARK: 1.2淘讥、添加日歷事件
/// 添加日歷事件
/// - Parameters:
/// - title: 提醒的標(biāo)題
/// - startDate: 開始時(shí)間
/// - endDate: 結(jié)束時(shí)間
/// - notes: 備注
/// - eventsClosure: 事件閉包
static func addCalendarsEvents(title: String, startDate: Date, endDate: Date, notes: String, eventsClosure: @escaping ((Bool, String?) -> Void)) {
let eventStore = EKEventStore()
eventStore.requestAccess(to: .event, completion: {
granted, error in
if (granted) && (error == nil) {
let event: EKEvent = EKEvent(eventStore: eventStore)
event.title = title
event.startDate = startDate
event.endDate = endDate
event.notes = notes
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.save(event, span: .thisEvent)
DispatchQueue.main.async {
eventsClosure(true, event.calendarItemIdentifier)
}
} catch {
DispatchQueue.main.async {
eventsClosure(false, nil)
}
}
} else {
DispatchQueue.main.async {
eventsClosure(false, nil)
}
}
})
}
// MARK: 1.3圃伶、修改日歷事件
/// 修改日歷事件
/// - Parameters:
/// - eventIdentifier: 唯一標(biāo)識(shí)符區(qū)分某個(gè)事件
/// - title: 提醒的標(biāo)題
/// - startDate: 開始時(shí)間
/// - endDate: 結(jié)束時(shí)間
/// - notes: 備注
/// - eventsClosure: 事件閉包
static func updateCalendarsEvents(eventIdentifier: String, title: String, startDate: Date, endDate: Date, notes: String, eventsClosure: @escaping ((Bool) -> Void)) {
let eventStore = EKEventStore()
// 請求日歷事件
eventStore.requestAccess(to: .event, completion: {
granted, error in
if (granted) && (error == nil) {
// 獲取本地日歷(剔除節(jié)假日,生日等其他系統(tǒng)日歷)
let calendars = eventStore.calendars(for: .event).filter({
(calender) -> Bool in
return calender.type == .local || calender.type == .calDAV
})
let predicate = eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: calendars)
let events = eventStore.events(matching: predicate)
let eventArray = events.filter { $0.calendarItemIdentifier == eventIdentifier }
guard eventArray.count > 0 else {
DispatchQueue.main.async {
eventsClosure(false)
}
return
}
let event = eventArray[0]
event.title = title
event.startDate = startDate
event.endDate = endDate
event.notes = notes
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.save(event, span: .thisEvent)
DispatchQueue.main.async {
eventsClosure(true)
}
} catch {
DispatchQueue.main.async {
eventsClosure(false)
}
}
} else {
DispatchQueue.main.async {
eventsClosure(false)
}
}
})
}
// MARK: 1.4、刪除日歷事件
/// 刪除日歷事件
/// - Parameters:
/// - eventIdentifier: 唯一標(biāo)識(shí)符區(qū)分某個(gè)事件
/// - eventsClosure: 事件閉包
static func removeCalendarsEvent(eventIdentifier: String, eventsClosure: @escaping ((Bool) -> Void)) {
let eventStore = EKEventStore()
// 請求日歷事件
eventStore.requestAccess(to: .event, completion: {
granted, error in
if (granted) && (error == nil) {
// 獲取本地日歷(剔除節(jié)假日窒朋,生日等其他系統(tǒng)日歷)
let calendars = eventStore.calendars(for: .event).filter({
(calender) -> Bool in
return calender.type == .local || calender.type == .calDAV
})
// 獲取當(dāng)前年
let com = Calendar.current.dateComponents([.year], from: Date())
let currentYear = com.year!
var events: [EKEvent] = []
// 獲取所有的事件(前后20年)
for i in -20...20 {
let startDate = startOfMonth(year: currentYear + i, month:1)
let endDate = endOfMonth(year: currentYear + i, month: 12, returnEndTime: true)
let predicate = eventStore.predicateForEvents(
withStart: startDate, end: endDate, calendars: calendars)
let eV = eventStore.events(matching: predicate)
events.append(eV)
}
let event = events.filter { return $0.calendarItemIdentifier == eventIdentifier }
guard event.count > 0 else {
DispatchQueue.main.async {
eventsClosure(false)
}
return
}
do {
try eventStore.remove(event[0], span: .thisEvent, commit: true)
DispatchQueue.main.async {
eventsClosure(true)
}
} catch {
DispatchQueue.main.async {
eventsClosure(false)
}
}
} else {
DispatchQueue.main.async {
eventsClosure(false)
}
}
})
}
}
二搀罢、提醒事件的基本的使用
public extension JKEKEvent {
// MARK: 2.1、查詢出所有提醒事件
static func selectReminder(remindersClosure: @escaping (([EKReminder]?) -> Void)) {
// 在取得提醒之前侥猩,需要先獲取授權(quán)
let eventStore = EKEventStore()
eventStore.requestAccess(to: .reminder) {
(granted: Bool, error: Error?) in
if (granted) && (error == nil) {
// 獲取授權(quán)后榔至,我們可以得到所有的提醒事項(xiàng)
let predicate = eventStore.predicateForReminders(in: nil)
eventStore.fetchReminders(matching: predicate, completion: {
(reminders: [EKReminder]?) -> Void in
DispatchQueue.main.async {
remindersClosure(reminders)
}
})
} else {
DispatchQueue.main.async {
remindersClosure(nil)
}
}
}
}
// MARK: 2.2、添加提醒事件
/// 添加提醒事件
/// - Parameters:
/// - title: 提醒的標(biāo)題
/// - startDate: 開始時(shí)間
/// - endDate: 結(jié)束時(shí)間
/// - notes: 備注
/// - eventsClosure: 事件閉包
static func addReminder(title: String, startDate: Date, endDate: Date, notes: String, eventsClosure: @escaping ((Bool, String?) -> Void)) {
let eventStore = EKEventStore()
// 獲取"提醒"的訪問授權(quán)
eventStore.requestAccess(to: .reminder) {(granted, error) in
if (granted) && (error == nil) {
// 創(chuàng)建提醒條目
let reminder = EKReminder(eventStore: eventStore)
reminder.title = title
reminder.notes = notes
reminder.startDateComponents = dateComponentFrom(date: startDate)
reminder.dueDateComponents = dateComponentFrom(date: endDate)
reminder.calendar = eventStore.defaultCalendarForNewReminders()
// 保存提醒事項(xiàng)
do {
try eventStore.save(reminder, commit: true)
DispatchQueue.main.async {
eventsClosure(true, reminder.calendarItemIdentifier)
}
} catch {
DispatchQueue.main.async {
eventsClosure(false, nil)
}
}
} else {
DispatchQueue.main.async {
eventsClosure(false, nil)
}
}
}
}
// MARK: 2.3欺劳、修改提醒事件
/// 修改提醒事件
/// - Parameters:
/// - eventIdentifier: 唯一標(biāo)識(shí)符區(qū)分某個(gè)事件
/// - title: 提醒的標(biāo)題
/// - startDate: 開始時(shí)間
/// - endDate: 結(jié)束時(shí)間
/// - notes: 備注
/// - eventsClosure: 事件閉包
static func updateEvent(eventIdentifier: String, title: String, startDate: Date, endDate: Date, notes: String, eventsClosure: @escaping ((Bool) -> Void)) {
let eventStore = EKEventStore()
// 獲取"提醒"的訪問授權(quán)
eventStore.requestAccess(to: .reminder) {(granted, error) in
if (granted) && (error == nil) {
// 獲取授權(quán)后唧取,我們可以得到所有的提醒事項(xiàng)
let predicate = eventStore.predicateForReminders(in: nil)
eventStore.fetchReminders(matching: predicate, completion: {
(reminders: [EKReminder]?) -> Void in
guard let weakReminders = reminders else {
DispatchQueue.main.async {
eventsClosure(false)
}
return
}
let weakReminder = weakReminders.filter { $0.calendarItemIdentifier == eventIdentifier }
guard weakReminder.count > 0 else {
DispatchQueue.main.async {
eventsClosure(false)
}
return
}
let reminder = weakReminder[0]
reminder.title = title
reminder.notes = notes
reminder.startDateComponents = dateComponentFrom(date: startDate)
reminder.dueDateComponents = dateComponentFrom(date: endDate)
reminder.calendar = eventStore.defaultCalendarForNewReminders()
// 修改提醒事項(xiàng)
do {
try eventStore.save(reminder, commit: true)
DispatchQueue.main.async {
eventsClosure(true)
}
} catch {
DispatchQueue.main.async {
eventsClosure(false)
}
}
})
}
}
}
// MARK: 2.4、移除提醒事件
/// 移除提醒事件
/// - Parameters:
/// - eventIdentifier: 唯一標(biāo)識(shí)符區(qū)分某個(gè)事件
/// - title: 提醒的標(biāo)題
/// - startDate: 開始時(shí)間
/// - endDate: 結(jié)束時(shí)間
/// - notes: 備注
/// - eventsClosure: 事件閉包
static func removeEvent(eventIdentifier: String, eventsClosure: @escaping ((Bool) -> Void)) {
let eventStore = EKEventStore()
// 獲取"提醒"的訪問授權(quán)
eventStore.requestAccess(to: .reminder) {(granted, error) in
if (granted) && (error == nil) {
// 獲取授權(quán)后划提,我們可以得到所有的提醒事項(xiàng)
let predicate = eventStore.predicateForReminders(in: nil)
eventStore.fetchReminders(matching: predicate, completion: {
(reminders: [EKReminder]?) -> Void in
guard let weakReminders = reminders else {
DispatchQueue.main.async {
eventsClosure(false)
}
return
}
let reminderArray = weakReminders.filter { $0.calendarItemIdentifier == eventIdentifier }
guard reminderArray.count > 0 else {
DispatchQueue.main.async {
eventsClosure(false)
}
return
}
// 移除提醒事項(xiàng)
do {
try eventStore.remove(reminderArray[0], commit: true)
DispatchQueue.main.async {
eventsClosure(true)
}
} catch {
DispatchQueue.main.async {
eventsClosure(false)
}
}
})
}
}
}
}
// MARK:- private
private extension JKEKEvent {
/// 根據(jù)NSDate獲取對應(yīng)的DateComponents對象
static func dateComponentFrom(date: Date) -> DateComponents {
let cal = Calendar.current
let dateComponents = cal.dateComponents([.minute, .hour, .day, .month, .year], from: date)
return dateComponents
}
/// 指定年月的開始日期
static func startOfMonth(year: Int, month: Int) -> Date {
let calendar = Calendar.current
var startComps = DateComponents()
startComps.day = 1
startComps.month = month
startComps.year = year
let startDate = calendar.date(from: startComps)!
return startDate
}
/// 指定年月的結(jié)束日期
static func endOfMonth(year: Int, month: Int, returnEndTime: Bool = false) -> Date {
let calendar = Calendar.current
var components = DateComponents()
components.month = 1
if returnEndTime {
components.second = -1
} else {
components.day = -1
}
let tem = startOfMonth(year: year, month:month)
let endOfYear = calendar.date(byAdding: components, to: tem)!
return endOfYear
}
}