7 Replies
      Latest reply on Aug 6, 2019 2:17 PM by SpaceMan
      SpaceMan Level 1 Level 1 (10 points)

        Anybody know how to do that?

        • Re: Presenting a EKEventEditViewController from SwiftUI View
          caram Level 1 Level 1 (0 points)

          Does this help?

          https://stackoverflow.com/questions/56784722/swiftui-send-email

           

          I'd be interested in your code once you figure it out.

          • Re: Presenting a EKEventEditViewController from SwiftUI View
            SpaceMan Level 1 Level 1 (10 points)

            I was able to bring up the EKEventEditViewConttroller, but no delegate, so could not handle the result.

            • Re: Presenting a EKEventEditViewController from SwiftUI View
              SpaceMan Level 1 Level 1 (10 points)

              The trick is to embed it inside a ZStack and make it appear via an @Binding.

               

              import Foundation
              import SwiftUI
              import EventKitUI
              
              let eventStore = EKEventStore()
              struct EKEventWrapper: UIViewControllerRepresentable {
                  @Binding var isShown: Bool
                  typealias UIViewControllerType = EKEventEditViewController
                  var theEvent = EKEvent.init(eventStore: eventStore)
                  
                  func makeUIViewController(context: UIViewControllerRepresentableContext<EKEventWrapper>) -> EKEventEditViewController {
              //        func makeUIViewController(context: UIViewControllerRepresentableContext<EKEventWrapper>) -> EKEventWrapper.UIViewControllerType {
                      theEvent.startDate = Date()
                      theEvent.endDate = Date()
                      theEvent.title = "The Main Event!"
                      let calendar = EKCalendar.init(for: .event, eventStore: eventStore)
                      theEvent.calendar = calendar
                      let controller = EKEventEditViewController()
                      controller.event = theEvent
                      controller.eventStore = eventStore
                      controller.editViewDelegate = context.coordinator
                      return controller
                  }
              
                  func updateUIViewController(_ uiViewController: EKEventWrapper.UIViewControllerType, context: UIViewControllerRepresentableContext<EKEventWrapper>) {
                      //
                  }
                  
                  func makeCoordinator() -> EKEventWrapper.Coordinator {
                      return Coordinator(isShown: $isShown, event: theEvent)
                  }
                    
                  class Coordinator : NSObject, UINavigationControllerDelegate, EKEventEditViewDelegate {
                      @Binding var isShown: Bool
                      
                      init(isShown: Binding<Bool>, event: EKEvent) {
                          _isShown = isShown
                      }
                      
                      func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
                          switch action {
                          case .canceled:
                              print("Canceled")
                              isShown = false
              //                controller.dismiss(animated: true, completion: nil)
                          case .saved:
                              print("Saved")
                              do {
                                  try controller.eventStore.save(controller.event!, span: .thisEvent, commit: true)
                              }
                              catch {
                                  print("Problem saving event")
                              }
                              isShown = false
              //                controller.dismiss(animated: true, completion: nil)
                          case .deleted:
                              print("Deleted")
                              isShown = false
              //                controller.dismiss(animated: true, completion: nil)
                          @unknown default:
                              print("I shouldn't be here")
                              isShown = false
              //                controller.dismiss(animated: true, completion: nil)
                          }
                      }
                  }
              }