SwiftUI EKEventEditViewController 字段不可编辑

SwiftUI EKEventEditViewController fields not editable

我一直在尝试将 EKEventEditViewController 与 SwiftUI 一起使用。我尝试遵循开发论坛的一些建议,了解如何在带有协调器的 UIViewControllerRepresentable 包装器的帮助下添加它。我正在添加下面的代码。

EKEventEditViewController 显示正确,但我面临的问题是只有部分字段是可编辑的。我附上了一张显示互动的 gif。

有人遇到过这个问题吗? 这是代码:

import Foundation
import SwiftUI
import EventKitUI

let eventStore = EKEventStore()

struct NewEventGenerator: UIViewControllerRepresentable {

typealias UIViewControllerType = EKEventEditViewController

@Binding var isShowing: Bool

var theEvent = EKEvent.init(eventStore: eventStore)

func makeUIViewController(context: UIViewControllerRepresentableContext<NewEventGenerator>) -> EKEventEditViewController {

    let controller = EKEventEditViewController()
    controller.event = theEvent
    controller.eventStore = eventStore
    controller.editViewDelegate = context.coordinator

    return controller
}

func updateUIViewController(_ uiViewController: NewEventGenerator.UIViewControllerType, context: UIViewControllerRepresentableContext<NewEventGenerator>) {
    uiViewController.view.backgroundColor = .red
}


func makeCoordinator() -> Coordinator {
    return Coordinator(isShowing: $isShowing)
}

class Coordinator : NSObject, UINavigationControllerDelegate, EKEventEditViewDelegate {

    @Binding var isVisible: Bool

    init(isShowing: Binding<Bool>) {
        _isVisible = isShowing
    }

    func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
        switch action {
        case .canceled:
            isVisible = false
        case .saved:
            do {
                try controller.eventStore.save(controller.event!, span: .thisEvent, commit: true)
            }
            catch {
                print("Event couldn't be created")
            }
            isVisible = false
        case .deleted:
            isVisible = false
        @unknown default:
            isVisible = false
        }
    }
}}

与 Xcode 12 / iOS 14 一起正常工作。从字面上复制粘贴您的代码,在 Info.plist.

中添加了请求访问和描述

经过全面测试的模块,如果有什么可能有用的话。

import SwiftUI
import EventKitUI

let eventStore = EKEventStore()

struct NewEventGenerator: UIViewControllerRepresentable {
    typealias UIViewControllerType = EKEventEditViewController

    @Binding var isShowing: Bool
    var theEvent: EKEvent

    init(isShowing: Binding<Bool>) {
        eventStore.requestAccess(to: .event) { allow, error in
            print("Result: \(allow) or [\(error.debugDescription)]")
        }

        theEvent = EKEvent.init(eventStore: eventStore)

        _isShowing = isShowing
    }


func makeUIViewController(context: UIViewControllerRepresentableContext<NewEventGenerator>) -> EKEventEditViewController {

    let controller = EKEventEditViewController()
    controller.event = theEvent
    controller.eventStore = eventStore
    controller.editViewDelegate = context.coordinator

    return controller
}

func updateUIViewController(_ uiViewController: NewEventGenerator.UIViewControllerType, context: UIViewControllerRepresentableContext<NewEventGenerator>) {
    uiViewController.view.backgroundColor = .red
}


func makeCoordinator() -> Coordinator {
    return Coordinator(isShowing: $isShowing)
}

class Coordinator : NSObject, UINavigationControllerDelegate, EKEventEditViewDelegate {

    @Binding var isVisible: Bool

    init(isShowing: Binding<Bool>) {
        _isVisible = isShowing
    }

    func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
        switch action {
        case .canceled:
            isVisible = false
        case .saved:
            do {
                try controller.eventStore.save(controller.event!, span: .thisEvent, commit: true)
            }
            catch {
                print("Event couldn't be created")
            }
            isVisible = false
        case .deleted:
            isVisible = false
        @unknown default:
            isVisible = false
        }
    }
}}

struct TestEventKitViewInSheet: View {     // just created in ContentView body
    @State private var showIt = false
    var body: some View {
        Button("Events") { showIt = true }
            .sheet(isPresented: $showIt) {
                NewEventGenerator(isShowing: $showIt)
            }
    }
}