1.配置info.plist
与其他私有内容一样,第一步是配置Info.plist然后请求访问。
要保存事件,您需要添加密钥NSCalendarsUsageDescription并Info.plist解释为什么需要访问日历。
2.引入EventKit

3.获取权限
granted 只代表是否 授权

同时也提供了获取用户授权状态的api, 不会触发授权弹窗

4.创建event
let event = EKEvent(eventStore: eventStore)
event.title = "Workout for Women: Workout Reminder"
event.location = "Good things come to those who sweat. All it takes is 7 minutes!"
event.startDate = startDate
event.endDate = startDate
event.calendar = calendar
event.url = URL(string: calendarOpenUrl)
event.addAlarm(.init(relativeOffset: 1))
event.addRecurrenceRule
do {
try eventStore.save(event, span: .thisEvent, commit: true)
userSave(key: caldendarLastDate, value: realHourAndMinDate)
eventIdenti = event.eventIdentifier
} catch {
print(error)
}
url通过点击唤起本地app
alarm 设置时间到了是否有提示
startdate 开始的时间
addRecurrenceRule: 给这个event添加循环, 如果循环内 不包括现在的日期, 那么这个event。也会设置
eventIdentifier: 只能获取, 可以存储到本地, 删除的时候可以删除指定eventIdentifier的 event

5.删除
批量删除,获取到对应的时间节点, 然后批量删除特定的event,当event删除过多的时候会很耗时
也可以根据特定的eventid 进行删除
let endDate = Calendar.current.date(bySettingHour: 23, minute: 59, second: 59, of: Date().endOfWeek) ?? Date()
let predicate = eventStore.predicateForEvents(withStart: Date().startOfWeek, end: endDate, calendars: nil)
for item in eventStore.events(matching: predicate) {
if item.url?.absoluteString == calendarOpenUrl {
try? eventStore.remove(item, span: .thisEvent, commit: true)
}
}
网友评论