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()
}
|