加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift - 使用EventKit获取系统日历事件,添加事件

发布时间:2020-12-14 07:17:28 所属栏目:百科 来源:网络整理
导读:通过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
通过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" )
}catch{}
// 获取所有的事件(前后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{

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读