Last active
March 13, 2017 07:11
-
-
Save robertmryan/ac7e58c662dc735c3c32fbd70355d9fc to your computer and use it in GitHub Desktop.
Swift 3 iOS code to add event to calendar (http://stackoverflow.com/questions/42754868/save-event-to-users-calendar)
This file contains hidden or 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 | |
| import UIKit | |
| import EventKit | |
| import EventKitUI | |
| class ViewController: UIViewController { | |
| let store = EKEventStore() | |
| @IBAction func didTapButton(_ sender: AnyObject) { | |
| checkAuthorization { | |
| self.createEvent() | |
| } | |
| } | |
| /// Check calendar authorization | |
| /// | |
| /// - If authorized, just call the provided closure; | |
| /// - If not yet determined, perform the standard `requestAccess` process; | |
| /// - If denied/restricted, tell user it's required and ask if they want to go to settings to authorize. | |
| /// | |
| /// - Parameter authorizedSuccessBlock: The closure to call once permission is confirmed. If never confirmed, the closure is not called. | |
| private func checkAuthorization(authorized: @escaping () -> ()) { | |
| let status = EKEventStore.authorizationStatus(for: .event) | |
| switch status { | |
| case .denied, .restricted: | |
| promptToOpenSettings() | |
| case .authorized: | |
| authorized() | |
| case .notDetermined: | |
| store.requestAccess(to: .event) { granted, error in | |
| DispatchQueue.main.async { | |
| guard granted else { | |
| self.promptToOpenSettings() | |
| return | |
| } | |
| authorized() | |
| } | |
| } | |
| } | |
| } | |
| /// Ask user whether they want to allow access to settings | |
| private func promptToOpenSettings() { | |
| let alert = UIAlertController(title: nil, message: "This app cannot add event to your calendar until you grant the app permission to do so. Would you like to go to Settings to enable calendar access?", preferredStyle: .actionSheet) | |
| alert.addAction(UIAlertAction(title: "Go to Settings", style: .default) { action in | |
| UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!, options: [:], completionHandler: nil) | |
| }) | |
| alert.addAction(UIAlertAction(title: "Cancel", style: .cancel)) | |
| self.present(alert, animated: true) | |
| } | |
| /// Create the event | |
| private func createEvent() { | |
| // create the event object | |
| let event = EKEvent(eventStore: store) | |
| event.title = "Foo" | |
| event.startDate = Date().addingTimeInterval(60 * 60) // create event for one hour from now | |
| event.endDate = event.startDate.addingTimeInterval(60 * 60) // and it's one hour long | |
| // prompt user to add event (to whatever calendar they want) | |
| let controller = EKEventEditViewController() | |
| controller.event = event | |
| controller.eventStore = store | |
| controller.editViewDelegate = self | |
| present(controller, animated: true) | |
| } | |
| } | |
| extension ViewController: EKEventEditViewDelegate { | |
| func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) { | |
| controller.dismiss(animated: true) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note, this Swift 3 code: