[英]How to return the EKEvent.identifier right after saving it to the EKEventStore?
I want to write a method, that takes a event object, wich has a name and a date parameters.我想编写一个方法,它接受一个事件对象,它有一个名称和一个日期参数。 The function requests access / checks access to the event store, creates an EKEvent with the parameters, saves it to the store and then returns the eventidentifier as a String.
该函数请求访问/检查对事件存储的访问,创建带有参数的 EKEvent,将其保存到存储中,然后将事件标识符作为字符串返回。
So far I have trouble because the eventStore.requestAccess(To:) methods closure escapes, and the string is returned befor the EKEvent object actually is created and saved to the store.到目前为止,我遇到了麻烦,因为 eventStore.requestAccess(To:) 方法闭包转义了,并且在实际创建 EKEvent 对象并将其保存到商店之前返回了字符串。
My method sits in the code of my EventHelper class, that is the abstraction layer between my EventStore and Apples EKEventStore.我的方法位于我的 EventHelper 类的代码中,这是我的 EventStore 和 Apples EKEventStore 之间的抽象层。
import EventKit
struct Event {
var name: String
var date: Date
var id: String?
}
class EventHelper {
// MARK: Properties
var store: EKEventStore!
// MARK: Methods
func createCalendarEvent(for event: Event) -> String? {
// Prepare a place to store the eventIdentifier
var identifier : String?
// Get acces to the eventstore
store.requestAccess(to: .event) { (granted, error) in
if (granted) && (error == nil) {
print("Calendar event creation.")
print("granted: \(granted)")
print("error: \(String(describing: error))")
// Create a new event kit event
let newEvent = EKEvent(eventStore: self.store)
newEvent.title = event.name
newEvent.startDate = event.date
// Create a timeinterval for the end date
let twoHourTimeInterval = TimeInterval(exactly: 7200)
newEvent.endDate = event.date.addingTimeInterval(twoHourTimeInterval!)
// choose the calender the event should be assinged to
newEvent.calendar = self.store.defaultCalendarForNewEvents
// try to save the new event in the event store
do {
try self.store.save(newEvent, span: .thisEvent, commit: true)
identifier = newEvent.eventIdentifier
print("Saved event with ID: \(String(describing: newEvent.eventIdentifier))")
// The event gets created and the ID is printed to the console but at a time when the whole function already has returned (nil)
} catch let error as NSError {
print("Failed to save event with error: \(error)")
}
}
else {
print("Failed to save event with error \(String(describing: error)) or access not granted")
}
}
print("new Event: \(String(describing: identifier))")
return identifier
}
}
I found a solution!我找到了解决办法!
Instead of using the .requestAccess(to:completion:)
for the event creation.而不是使用
.requestAccess(to:completion:)
来创建事件。 I only use it when I explicitly need to ask for access to the EKEventStore.我只在我明确需要访问 EKEventStore 时使用它。 And to check that I switch on
.authorizationStatus(for:)
并检查我是否打开
.authorizationStatus(for:)
Here comes the code:代码来了:
import EventKit
class EventHelper {
// MARK: Properties
var store: EKEventStore!
// MARK: Methods
/*
This thing I got from here https://stackoverflow.com/questions/28379603/how-to-add-an-event-in-the-device-calendar-using-swift more or less
*/
func createCalendarEvent(for event: Event) -> String? {
// Act base upon the current authorisation status
switch EKEventStore.authorizationStatus(for: .event) {
case EKAuthorizationStatus.notDetermined:
// Ask for permission
requestAuthorisationToCalendar()
// And try again
return try createCalendarEvent(for: event)
case EKAuthorizationStatus.denied:
print("Access to the Event Store was denied.")
case EKAuthorizationStatus.restricted:
print("Access to the Event Store was restricted.")
case EKAuthorizationStatus.authorized:
// Create a new event
let newEvent = EKEvent(eventStore: store)
newEvent.title = event.name
newEvent.startDate = event.date
// Create a timeinterval for the end date
let twoHourTimeInterval = TimeInterval(exactly: 7200)
newEvent.endDate = event.date.addingTimeInterval(twoHourTimeInterval!)
// choose the calender the event should be assinged to
newEvent.calendar = self.store.defaultCalendarForNewEvents
// try to save the new event in the event store
do {
try self.store.save(newEvent, span: .thisEvent, commit: true)
print("Saved event with ID: \(String(describing: newEvent.eventIdentifier))")
return newEvent.eventIdentifier
} catch let error as NSError {
print("Failed to save event with error: \(error)")
return nil
}
}
}
// MARK: Privat Methods
private func requestAuthorisationToCalendar() {
store.requestAccess(to: .event) { (granted, error) in
if (granted) && (error == nil) {
DispatchQueue.main.async {
print("User has granted access to calendar")
}
} else {
DispatchQueue.main.async {
print("User has denied access to calendar")
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.