Last active
July 21, 2018 14:40
-
-
Save longvudai/8360b8769b07262dcf1270abdd859b24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// EventKit | |
// | |
// Created by Dai Long on 7/21/18. | |
// Copyright © 2018 Dai Long. All rights reserved. | |
// | |
import UIKit | |
import EventKit | |
import EventKitUI | |
class Task { | |
var title: String | |
var isCompleted: Bool = false | |
init(title: String) { | |
self.title = title | |
} | |
} | |
class ViewController: UIViewController { | |
// MARK: - IBOutlets | |
@IBOutlet weak var tableView: UITableView! | |
var isAccessToEventStoreGranted: Bool = false | |
var eventStore: EKEventStore = EKEventStore() | |
var calendars: [EKCalendar] = [] | |
var tasks: [Task] = [] | |
func updateAuthorizationStatusToAccessEventStore() { | |
let authorizationStatus = EKEventStore.authorizationStatus(for: .event) | |
switch authorizationStatus { | |
case .authorized: | |
print("The app is authorized to access the service.") | |
case .denied, .restricted: | |
print("The user explicitly denied access to the service for the app.") | |
case .notDetermined: | |
print("The user has not yet made a choice regarding whether the app may access the service.") | |
self.isAccessToEventStoreGranted = true | |
eventStore.requestAccess(to: .reminder) { (isAccessToEventStoreGranted, error: Error?) in | |
if let error = error { | |
print("Error: \(error.localizedDescription)") | |
return | |
} | |
if isAccessToEventStoreGranted == true { | |
// self.loadCalendar() | |
} | |
} | |
} | |
} | |
func loadCalendar() { | |
calendars = eventStore.calendars(for: .event) | |
// tableView.reloadData() | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
setupTableView() | |
updateAuthorizationStatusToAccessEventStore() | |
for i in 1...10 { | |
let task = Task(title: "title \(i)") | |
tasks.append(task) | |
} | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
extension ViewController: UITableViewDelegate, UITableViewDataSource { | |
fileprivate func setupTableView() { | |
tableView.delegate = self | |
tableView.dataSource = self | |
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
//return calendars.count | |
return tasks.count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) | |
cell.textLabel?.text = tasks[indexPath.row].title | |
// cell.textLabel?.text = calendars[indexPath.row].title | |
return cell | |
} | |
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
let cell = tableView.cellForRow(at: indexPath) | |
let reminder = EKReminder.init(eventStore: eventStore) | |
reminder.title = cell?.textLabel?.text ?? "default" | |
reminder.calendar = eventStore.defaultCalendarForNewReminders() | |
let date = Date.init().addingTimeInterval(60) | |
let alarm = EKAlarm.init(absoluteDate: date) | |
reminder.addAlarm(alarm) | |
do { | |
try eventStore.save(reminder, commit: true) | |
print("save success!") | |
} | |
catch let error { | |
print("Couldnt Save: \(error.localizedDescription)") | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment