Robatson
Robatson

Reputation: 23

EKEventViewController wont show Edit Button on iOS 13

i got a simple app where i want to present an event within an EKEventViewController.

    // the button action which validates if the event store access is granted and presents the given alert if true
    @IBAction func actionButtonShowPopover(_ sender: Any) {
        eventStore.requestAccess(to: .event) { (granted, _) in
            guard granted else { return }

            let event = self.generateAndSaveEvent()
            self.presentEventViewController(withEvent: event)
        }
    }
    // creates and tries to save an sample even and returns it
    private func generateAndSaveEvent() -> EKEvent {
        let event = EKEvent(eventStore: eventStore)

        event.title = "Event Title"
        event.startDate = Date()
        event.endDate = Date().addingTimeInterval(1800)
        event.calendar = eventStore.defaultCalendarForNewEvents

        do {
            try eventStore.save(event, span: .thisEvent)
        } catch(let error) {
            print(error)
        }

        return event
    }
    // displays an EKEventViewController with our newly created event within an popover
    private func presentEventViewController(withEvent event: EKEvent) {
        DispatchQueue.main.async {
            let eventVC = EKEventViewController()

            eventVC.event = event
            eventVC.allowsEditing = true

            eventVC.modalPresentationStyle = .popover
            eventVC.popoverPresentationController?.sourceView = self.buttonShowPopover
            eventVC.popoverPresentationController?.sourceRect = self.buttonShowPopover.frame.offsetBy(dx: 0, dy: -10)
            eventVC.popoverPresentationController?.backgroundColor = .white
            eventVC.popoverPresentationController?.permittedArrowDirections = .up

            self.present(eventVC, animated: false, completion: nil)
        }
    }

i created an event as shown in the code above and simply displaying it within the popover view controller. since ios 13 i got a different result:

iOS 12.4 with edit button

iOS 13 without edit button

is there any chance i'm missing changes from iOS12 -> iOS13? thanks upfront - i'm grateful for any advice!

Upvotes: 2

Views: 484

Answers (2)

pjh68
pjh68

Reputation: 443

In my app, that's been around for a while, I was experiencing the same problem with the edit button no longer appearing in iOS13. Unlike other users, my EKEventViewController was already wrapped in a navigation controller, so it wasn't that issue.

Several hours of going down rabbit holes, I've found a fix. Here's where the problems were occurring in my app:

  • Debugging just before opening the view, the EKEvent object I was trying to edit didn't have a value set for .eventIdentifier. Reading into this, it seems this property is lazy loaded, so not being able to retrieve the value here suggests the link between the EKEvent and the EKStore I used to fetch it has been lost somewhere in the application lifecycle. This is a change that been introduced somewhere along the iOS/Swift upgrade journey - I can't pin down the change that has caused this.

  • By accessing the EKEvent.eventIdentifier at the time I first retrieved the EKEvent, I now had this identifier for later use

  • Before presenting the EKEventViewController, I fetch a fresh copy of this event and use the fresh event in the controller:

let freshEvent = store.event(withIdentifier: staleEvent.eventIdentifier) 
eventViewController.event = freshEvent

Upvotes: 1

nathanwhy
nathanwhy

Reputation: 6144

Edit button has moved to navigation bar in iOS 13. You need to present it without popover style.

Upvotes: 2

Related Questions