Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Last active March 13, 2017 07:11
Show Gist options
  • Select an option

  • Save robertmryan/ac7e58c662dc735c3c32fbd70355d9fc to your computer and use it in GitHub Desktop.

Select an option

Save robertmryan/ac7e58c662dc735c3c32fbd70355d9fc to your computer and use it in GitHub Desktop.
// 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)
}
}
@robertmryan

Copy link
Copy Markdown
Author

Note, this Swift 3 code:

  • performs "just in time" calendar authorization (you're more likely to get permission if you ask at the point the user is actively trying to undertake some activity to add something to their calendar ... if you ask at app startup, the user doesn't quite know your intentions and is more likely to deny permissions);
  • lets the user go to "Settings" if access was previously denied.

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