获取日历事件权限
导入 importEventKit
let eventStore = EKEventStore.init()
weak var weakSelf = self
eventStore.requestAccess(to: EKEntityType.event, completion: {(isAllow, error) in
if isAllow == true{
}else{
}
})
新增日历事件
let event = EKEvent.init(eventStore: eventStore)
event.title = “mm提醒” + “【拜访” + visitor + “】”
if visitStyle != nil{
event.notes = visitStyle
}
event.calendar = eventStore.defaultCalendarForNewEvents
event.calendar.cgColor = colorLiteral(red: 0.4941176471, green: 0.8078431373, blue: 0.4392156863, alpha: 1).cgColor
event.calendar.title = “mm”
event.startDate = Date()
event.endDate =Date()+ 3600
print(event.calendar.calendarIdentifier)
event.addAlarm(EKAlarm(relativeOffset: -60))
do {
try eventStore.save(event, span: .thisEvent, commit: true)
DispatchQueue.main.async {
//新增成功后的处理
}
} catch {
print(error)
}
查询和删除日历事件
let dfmatter = DateFormatter()
dfmatter.dateFormat=“yyyy-MM-dd HH:mm”
let startDate = dfmatter.date(from: self.model.return_visit_time!)!
weakSelf?.eventDate = startDate
let endDate = dfmatter.date(from: self.model.return_visit_time!)! + 3600
let predicate = self.eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: nil)
eventStore.enumerateEvents(matching: predicate, using: { (event, pointer) in
do {
let _ = try self.eventStore.remove(event, span: .thisEvent)
} catch {
print(error)
}
})
如果要删除指定日历事件
根据事件唯一标识符:event.calendar.calendarIdentifier,我们需要将生成事件的标识符保存,删除时予以对照即可。此外,日历事件无法编辑修改,如若要完成此效果,我们需删除旧事件、新增新事件来替换
网友评论