如何在Swift中使用EventKit保存和删除日历事件

piztneat  于 2023-08-02  发布在  Swift
关注(0)|答案(2)|浏览(180)

我是swift的新手,我一直在尝试使用EventKit和EventKitUI创建和删除日历事件,到目前为止,我已经能够创建事件,但我很难删除它们。如果你有任何建议,请让我知道!以下是我目前为止的ViewController代码:

import EventKit
import EventKitUI

class ViewController: UIViewController, EKEventViewDelegate {
    
    let store = EKEventStore()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(didTapAdd))

    }
    
    @objc func didTapAdd(){
        
        store.requestAccess(to: .event) {[weak self] success, error in
            if success, error == nil {
                DispatchQueue.main.async {
                    guard let store = self?.store else { return }
                    
                    // Describes the event to be added
                    let newEvent = EKEvent(eventStore: store)
                    newEvent.title = "My Event"
                    newEvent.startDate = Date()
                    newEvent.endDate = Date()
                    
                    // Lets user edit the event themselves, we dont want this?
                    //let otherVC = EKEventEditViewController()
                    //otherVC.eventStore = store
                    //otherVC.event = newEvent
                    //self?.present(otherVC,
                                  //animated: true,
                                  //completion: nil)
                    
                    let vc = EKEventViewController()
                    vc.delegate = self
                    vc.event = newEvent
                    let navVc = UINavigationController(rootViewController: vc)
                    self?.present(navVc, animated: true)
                }
            }
        }
    }

    
    func eventViewController(_ controller: EKEventViewController, didCompleteWith action: EKEventViewAction) {
        controller.dismiss(animated: true, completion: nil)
    }

    
}

字符串

4szc88ey

4szc88ey1#

首先,在启动应用程序时只执行requestAccess一次
第二,在代码中,你创建了一个新事件,但没有将其保存在存储中。
你得打电话

try eventStore.save(newEvent, commit: true)

字符串
删除工作非常相似。您需要EKEvent示例的引用,然后调用

try eventStore.remove(event, commit: true)


使用predicateForEvents(withStart:end:calendars:)events(matching:)获取示例

vcirk6k6

vcirk6k62#

正如vadian所说,尝试在一个示例中只请求一次访问权限--每次打开应用程序时检查访问权限是有意义的。您可能无法在删除访问权限后对其进行更改,但您可以告诉用户访问权限不起作用的原因。
我创建了两个函数:创建事件和删除事件。也许你可以使用它们:

var createdEvent: EKEvent?
func createCalendarEvent() {
        
        eventStore.requestAccess(to: .event) { (granted, error) in
            if granted && error == nil {
                
                let event = EKEvent(eventStore: self.eventStore)
                event.title = "The guide worked 😮"
                event.startDate = Date()
                event.endDate = Date().addingTimeInterval(7200)
                
                event.notes = "Follow more guides on softwareanders.com and create more apps."
                
                
                event.calendar = self.eventStore.defaultCalendarForNewEvents
                
                
                do {
                    try self.eventStore.save(event, span: .thisEvent)
                    
                    self.createdEvent = event
                } catch {
                    print("Error saving event: \(error.localizedDescription)")
                }
            } else {
                print("Access to calendar not granted or an error occurred.")
            }
        }
    }

func deleteEventById(eventId: String) {
if let eventToDelete = eventStore.event(withIdentifier: eventId) {
    do {
        try eventStore.remove(eventToDelete, span: .thisEvent)
    } catch {
        print("Error deleting event: \(error.localizedDescription)")
    }
}

字符串
}
如果你想了解更多关于EventKit的信息,我做了一个指南,你可以在这里创建,删除,编辑,获取一个和获取所有-link here

相关问题