Events 使用SwiftUI时如何请求访问日历?
我正在为我以前的一个应用程序采用SwiftUI,在某个时候我需要在iOS日历中添加一个事件。 我在Apple开发者论坛和Stack Overflow上做了一些研究,提出了以下解决方案,利用UIViewControllerRepresentable协议,以便仍然能够将UIViewControllerRepresentable与SwiftUI结合使用(您可以在下面的讨论链接中找到,供参考) 苹果开发者论坛讨论: 关于堆栈溢出的讨论: 我就快到了,但我现在缺少的是访问设备日历的授权状态控制,以及在需要时触发此类授权请求 在Swift中,这将是用来触发请求的函数,但我不确定如何在EKEventWrapper中处理它Events 使用SwiftUI时如何请求访问日历?,events,uiviewcontroller,calendar,authorization,swiftui,Events,Uiviewcontroller,Calendar,Authorization,Swiftui,我正在为我以前的一个应用程序采用SwiftUI,在某个时候我需要在iOS日历中添加一个事件。 我在Apple开发者论坛和Stack Overflow上做了一些研究,提出了以下解决方案,利用UIViewControllerRepresentable协议,以便仍然能够将UIViewControllerRepresentable与SwiftUI结合使用(您可以在下面的讨论链接中找到,供参考) 苹果开发者论坛讨论: 关于堆栈溢出的讨论: 我就快到了,但我现在缺少的是访问设备日历的授权状态控制,以及在
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
}
}
}
}
<代码>导入基础
导入快捷键
导入EventKitUI
让eventStore=EKEventStore()
结构EKEventWrapper:UIViewControllerRepresentable{
typealias UIViewControllerType=EKEventEditViewController
@绑定变量显示:Bool
var theEvent=EKEvent.init(eventStore:eventStore)
func makeUIViewController(上下文:UIViewControllerRepresentableContext)->EKEventEditViewController{
让calendar=EKCalendar.init(用于:.event,eventStore:eventStore)
theEvent.startDate=日期()
theEvent.endDate=日期()
theEvent.title=“会议”
theEvent.calendar=日历
let controller=EKEventEditViewController()
controller.event=theEvent
controller.eventStore=eventStore
controller.editViewDelegate=context.coordinator
返回控制器
}
func updateUIViewController(uViewController:EKEventWrapper.UIViewControllerType,上下文:UIViewControllerRepresentableContext){
//
}
func makeCoordinator()->Coordinator{
退货协调员(isShowing:$isShowing)
}
类协调器:NSObject、UINavigationControllerDelegate、EKEventEditViewDelegate{
@绑定变量显示:Bool
初始化(isShowing:绑定){
_isShowing=isShowing
}
func eventEditViewController(controller:EKEventEditViewController,didCompleteWith操作:EKEventEditViewAction){
开关动作{
案例。取消:
打印(“已取消”)
isShowing=false
案例。已保存:
打印(“已保存”)
做{
尝试controller.eventStore.save(controller.event!,span:.thisEvent,commit:true)
}
抓住{
打印(“保存问题事件”)
}
isShowing=false
案例.删除:
打印(“已删除”)
isShowing=false
@未知默认值:
打印(“我不应该在这里”)
isShowing=false
}
}
}
}
我相信下面的程序可以做到这一点,我已经在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")
}
}),,,,
我相信下面的内容可以做到这一点,我已经在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")
}
}),,,,
你解决了吗?你解决了吗?