如何在使用SwiftUI时请求访问日历?

问题描述 投票:1回答:1

我之前的一个应用采用了SwiftUI,在某些时候我需要将一个事件添加到iOS日历中,在Apple Developer Forums和Stack Overflow上做了一些研究,我得出了以下解决方案,利用UIViewControllerRepresentable协议,以便仍然能够在SwiftUI中使用UIViewControllers(你可以在下面找到讨论的链接,供你参考)。

苹果开发论坛上的讨论。https:/forums.developer.apple.comthread120372

在Stack Overflow上讨论。SwiftUI。发送邮件

我已经差不多了,但我现在缺少的是控制访问设备日历的授权状态,并在需要时触发授权请求。

在Swift中,这将是触发请求的函数,但我不知道如何在我的EKEventWrapper中处理这个问题。

func requestAccess(to entityType: EKEntityType, 
        completion: @escaping EKEventStoreRequestAccessCompletionHandler)

有什么好办法吗?

非常感谢

我到现在为止的解决方案。

import Foundation
import SwiftUI
import EventKitUI

let eventStore = EKEventStore()

struct EKEventWrapper: UIViewControllerRepresentable {

    typealias UIViewControllerType = EKEventEditViewController

    @Binding var isShowing: Bool

    var theEvent = EKEvent.init(eventStore: eventStore)

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

        let calendar = EKCalendar.init(for: .event, eventStore: eventStore)

        theEvent.startDate = Date()
        theEvent.endDate = Date()
        theEvent.title = "Meeting"
        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() -> Coordinator {
        return Coordinator(isShowing: $isShowing)
    }

    class Coordinator : NSObject, UINavigationControllerDelegate, EKEventEditViewDelegate {

        @Binding var isShowing: Bool

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

        func eventEditViewController(_ controller: EKEventEditViewController, didCompleteWith action: EKEventEditViewAction) {
            switch action {
            case .canceled:
                print("Canceled")
                isShowing = false                
            case .saved:
                print("Saved")
                do {
                    try controller.eventStore.save(controller.event!, span: .thisEvent, commit: true)
                }
                catch {
                    print("Problem saving event")
                }
                isShowing = false
            case .deleted:
                print("Deleted")
                isShowing = false
            @unknown default:
                print("I shouldn't be here")
                isShowing = false
            }
        }
    }
}
events uiviewcontroller calendar authorization swiftui
1个回答
0
投票

我相信下面的将做到这一点,我已经测试了它在SwiftUI使用一个按钮来操作的功能。第一次发帖,所以很抱歉,如果我完全偏离了目标!

import SwiftUI
import EventKit
import EventKitUI

struct calaccess: View {

var eventstore = EKEventStore()

var body: some View {

 func accesscalender(CalAccess: EKEventStore) {

    let eventstore = EKEventStore()

    eventstore.requestAccess(to: EKEntityType.event,completion:
               {(granted, error) in
            if !granted {
                print("Access to store not granted")
            }
    }),,,,
© www.soinside.com 2019 - 2024. All rights reserved.