-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalendarViewController.swift
114 lines (88 loc) · 3.79 KB
/
CalendarViewController.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// CalendarViewController.swift
// My Diabetic Manager
//
// Created by Vincenzo De Rosa on 03/02/17.
// Copyright © 2017 Vincenzo De Rosa. All rights reserved.
//
import UIKit
import EventKit
class CalendarViewController: UIViewController {
@IBOutlet weak var eventName: UITextField!
@IBOutlet weak var startDate: UIDatePicker!
@IBOutlet weak var endDate: UIDatePicker!
@IBOutlet weak var note: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
printEvents()
note.layer.borderWidth = 0.5
note.layer.cornerRadius = 7
note.layer.borderColor = UIColor.lightGray.cgColor
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addEventToCalendar(title: String, description: String?, startDate: NSDate, endDate: NSDate, completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil) {
let eventStore = EKEventStore()
eventStore.requestAccess(to: .event, completion: { (granted, error) in
if (granted) && (error == nil) {
let event = EKEvent(eventStore: eventStore)
event.title = title + "-MDM"
event.startDate = startDate as Date
event.endDate = endDate as Date
event.notes = description
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.save(event, span: .thisEvent)
} catch let e as NSError {
completion?(false, e)
return
}
completion?(true, nil)
} else {
completion?(false, error as NSError?)
}
})
}
@IBAction func saveEvent(_ sender: UIBarButtonItem) {
addEventToCalendar(title: eventName.text!, description: note.text!, startDate: startDate.date as NSDate, endDate: endDate.date as NSDate)
}
func printEvents(){
var titles : [String] = []
var startDates : [NSDate] = []
var endDates : [NSDate] = []
var descs : [String] = []
let eventStore = EKEventStore()
let calendars = eventStore.calendars(for: .event)
for calendar in calendars {
if calendar == eventStore.defaultCalendarForNewEvents {
let oneMonthAgo = NSDate(timeIntervalSinceNow: -30*24*3600)
let oneMonthAfter = NSDate(timeIntervalSinceNow: +30*24*3600)
let predicate = eventStore.predicateForEvents(withStart: oneMonthAgo as Date, end: oneMonthAfter as Date, calendars: [calendar])
let events = eventStore.events(matching: predicate)
for event in events {
if event.title.hasSuffix("-MDM"){
titles.append(event.title)
startDates.append(event.startDate as NSDate)
endDates.append(event.endDate as NSDate)
descs.append(event.notes!)
}
}
print(titles)
print(startDates)
print(endDates)
print(descs)
}
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}