Skip to content

Instantly share code, notes, and snippets.

@tmtk75
Created May 14, 2019 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmtk75/9a7a5f65e1459297951a61220bb5fbd1 to your computer and use it in GitHub Desktop.
Save tmtk75/9a7a5f65e1459297951a61220bb5fbd1 to your computer and use it in GitHub Desktop.
//import Cocoa
//var str = "Hello, playground"
import EventKit
let eventStore = EKEventStore()
func allowAuthorization() {
if getAuthorization_status() {
// 許可されている
return
} else {
// 許可されていない
eventStore.requestAccess(to:.event, completion: {
(granted, error) in
if granted {
return
}
else {
print("Not allowed")
}
})
}
}
// 認証ステータスを確認する
func getAuthorization_status() -> Bool {
// 認証ステータスを取得
let status = EKEventStore.authorizationStatus(for:.event)
// ステータスを表示 許可されている場合のみtrueを返す
switch status {
case .notDetermined:
print("NotDetermined")
return false
case .denied:
print("Denied")
return false
case .authorized:
print("Authorized")
return true
case .restricted:
print("Restricted")
return false
}
}
allowAuthorization()
// getAuthorization_status()
func viewDidLoad() {
// eventStore = EKEventStore()
eventStore.requestAccess(to: .reminder) { (granted, error) in
print("OK")
// let allReminders = EKReminder()
let cals = eventStore.calendars(for: .reminder)
let predicate = eventStore.predicateForIncompleteReminders(withDueDateStarting: nil, ending: nil, calendars: cals)
eventStore.fetchReminders(matching: predicate, completion: { (reminders: [EKReminder]?) -> Void in
print(reminders!)
var a: [[String:Any]] = []
for reminder: EKReminder? in reminders! as [EKReminder?] ?? [EKReminder?]() {
a.append(["title": reminder?.title as Any])
}
do {
let data = try JSONSerialization.data(withJSONObject: a)
let s = String(data: data, encoding: String.Encoding.utf8)
print(s!)
} catch let err {
print(err)
}
})
}
}
viewDidLoad()
print(".")
@tmtk75
Copy link
Author

tmtk75 commented May 14, 2019

Works on Playground, but doesn't work with swift command in iTerm2.
The latter returns empty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment