Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift - 使用EventKit获取系统日历事件,添加事件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

通过EventKit可以对iOS日历事件进行读取,添加等操作。但网上找到的都是使用Objective-C来编写的。 下面提供一个Swift版的样例,演示如何添加一个事件以及获取所有的事件列表。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 3
通过EventKit可以对iOS日历事件进行读取,添加等操作。但网上找到的都是使用Objective-C来编写的。
下面提供一个Swift版的样例,演示如何添加一个事件以及获取所有的事件列表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import UIKit
EventKit
class ViewController : UIViewController {
override func viewDidLoad() {
super .viewDidLoad()
let eventStore: EKEventStore = ()
// 'EKEntityType.Reminder' or 'EKEntityType.Event'
eventStore.requestAccessToEntityType(. Event ,completion: {
granted,error in
if (granted) && (error == nil ) {
print ( "granted \(granted)" )
"error \(error)" )
// 新建一个事件
event: EKEvent (eventStore: eventStore)
event.title = "新增一个测试事件"
event.startDate = NSDate ()
event.endDate = ()
event.notes = "这个是备注"
event.calendar = eventStore.defaultCalendarForNewEvents
do{
try eventStore.saveEvent(event,span: . ThisEvent )
"Saved Event" )
// 获取所有的事件(前后90天)
startDate= ().dateByAddingTimeInterval(-3600*24*90)
endDate= ().dateByAddingTimeInterval(3600*24*90)
predicate2 = eventStore.predicateForEventsWithStartDate(startDate,
endDate: endDate,calendars: )
"查询范围 开始:\(startDate) 结束:\(endDate)" )
eV = eventStore.eventsMatchingPredicate(predicate2) as [ ]!
eV != {
for i in eV {
"标题 \(i.title)" )
"开始时间: \(i.startDate)" )
"结束时间: \(i.endDate)" )
}
}
}
})
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}

EKEntityTypeEvent 已修改为EKEntityType.Reminder枚举
eventStore.saveEvent(event,span: EKSpanThisEvent,error: nil) 已修改
do{
try eventStore.saveEvent(event,span: .ThisEvent)
}catch{

}

大佬总结

以上是大佬教程为你收集整理的Swift - 使用EventKit获取系统日历事件,添加事件全部内容,希望文章能够帮你解决Swift - 使用EventKit获取系统日历事件,添加事件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: