Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
List your reminders in JSON
import Darwin
import EventKit
import Foundation
var debug = false
if ProcessInfo.processInfo.environment["DEBUG"] != nil {
debug = true
}
// https://stackoverflow.com/questions/28016578/how-to-create-a-date-time-stamp-and-format-as-iso-8601-rfc-3339-utc-time-zone
extension ISO8601DateFormatter {
convenience init(_ formatOptions: Options, timeZone: TimeZone = TimeZone(secondsFromGMT: 0)!) {
self.init()
self.formatOptions = formatOptions
self.timeZone = timeZone
}
}
extension Formatter { static let iso8601 = ISO8601DateFormatter([.withInternetDateTime, .withFractionalSeconds]) }
extension Date { var iso8601: String { return Formatter.iso8601.string(from: self) } }
extension String { var iso8601: Date? { return Formatter.iso8601.date(from: self) } }
let log = { (message: Any) -> Void in
if !debug {
return
}
let now = Date().iso8601
fputs(now + " -- " + (message as! String) + "\n", stderr)
}
let eventStore = EKEventStore()
let type = EKEntityType.reminder
func allowAuthorization() {
if getAuthorizationStatus() {
return
}
log("request access")
eventStore.requestAccess(to: type, completion: { (granted, error) in
if granted {
return
}
log("Not allowed")
exit(EXIT_FAILURE)
})
}
func getAuthorizationStatus() -> Bool {
let status = EKEventStore.authorizationStatus(for: type)
switch status {
case .notDetermined: log("NotDetermined"); return false
case .denied: log("Denied"); return false
case .authorized: log("Authorized"); return true
case .restricted: log("Restricted"); return false
default: log("Unknown"); return false
}
}
let conv = {(alarms: [EKAlarm]) -> [[String:Any]] in
var v: [[String:Any]] = []
for a: EKAlarm in alarms {
v.append([
"absoluteDate": a.absoluteDate!.iso8601,
"relativeOffset": a.relativeOffset,
// "type": a.type
// "emailAddress": a.emailAddress ?? nil
])
}
return v
}
let listReminders = {(group: DispatchGroup, completion: @escaping ([[String:Any]]) -> Void) in
eventStore.requestAccess(to: type) { (granted, error) in
let cals = eventStore.calendars(for: type)
let predicate = eventStore.predicateForIncompleteReminders(withDueDateStarting: nil, ending: nil, calendars: cals)
eventStore.fetchReminders(matching: predicate, completion: { (wr: [EKReminder]?) -> Void in
let reminders = wr ?? []
var a: [[String:Any]] = []
for r: EKReminder in reminders {
let alarms = conv(r.alarms ?? [])
a.append([
"_id": r.calendarItemIdentifier,
"calender": [
"title": r.calendar.title,
"_id": r.calendar.calendarIdentifier,
],
"title": r.title as Any,
// "url": r.url as Any,
"creationDate": (r.creationDate!.iso8601) as Any,
"location": r.location as Any,
"notes": r.notes as Any,
"alarms": alarms
])
}
completion(a)
group.leave()
})
}
}
let group = DispatchGroup()
group.enter()
allowAuthorization()
group.notify(queue: DispatchQueue.main) {
// print(DispatchQueue.main)
exit(EXIT_SUCCESS)
}
listReminders(group) { (a: [[String:Any]]) in
do {
let data = try JSONSerialization.data(withJSONObject: a)
let s = String(data: data, encoding: String.Encoding.utf8)
print(s!)
} catch let err {
log(err)
}
}
dispatchMain()
@tmtk75

This comment has been minimized.

Copy link
Owner Author

tmtk75 commented Jul 13, 2019

List all incomplete reminders in JSON.
My first meaningful swift code 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.