Calendar 不持久的
我可以使用以下功能创建新日历并保存它:Calendar 不持久的,calendar,swift3,eventkit,Calendar,Swift3,Eventkit,我可以使用以下功能创建新日历并保存它: func createCalendarForUser() { let sourcesInEventStore = self.eventStore.sources //works but doesn't persist let subscribedSourceIndex = sourcesInEventStore.index {$0.title == "Subscribed Calendars"} if let subscr
func createCalendarForUser() {
let sourcesInEventStore = self.eventStore.sources
//works but doesn't persist
let subscribedSourceIndex = sourcesInEventStore.index {$0.title == "Subscribed Calendars"}
if let subscribedSourceIndex = subscribedSourceIndex {
let userCalendar = EKCalendar(for: .event, eventStore: self.eventStore)
userCalendar.title = "newCalendar"
userCalendar.source = sourcesInEventStore[subscribedSourceIndex]
do {
try self.eventStore.saveCalendar(userCalendar, commit: true)
print("calendar creation successful")
} catch {
print("cal \(userCalendar.source.title) failed : \(error)")
}
}
}
当应用程序打开并运行时,此功能非常强大。我可以保存事件给他们,我可以看到他们在我的本地日历,生活是美好的。但是,一旦应用程序终止并进入后台,日历以及其中创建的任何事件都将消失。我曾尝试将日历保存到不同的源,而不是订阅的日历
源,但当我这样做时,日历甚至不会首先保存。以下是使用本地源的尝试之一:
func createCalendarForUser() {
let sourcesInEventStore = self.eventStore.sources
//never saves calendar
let localSourceIndex = sourcesInEventStore.index {$0.sourceType == .local}
if let localSourceIndex = localSourceIndex {
let userCalendar = EKCalendar(for: .event, eventStore: self.eventStore)
userCalendar.title = "newCalendar"
userCalendar.source = sourcesInEventStore[localSourceIndex]
do {
try self.eventStore.saveCalendar(userCalendar, commit: true)
print("creating new calendar successful")
} catch {
print("creating new calendar failed : \(error)")
}
}
}
我也尝试过这种方法:
func createCalendarForUser() {
let sourcesInEventStore = self.eventStore.sources
//doesnt work
let userCalendar = EKCalendar(for: .event, eventStore: self.eventStore)
userCalendar.title = "newCalendar"
userCalendar.source = sourcesInEventStore.filter{
(source: EKSource) -> Bool in
source.sourceType.rawValue == EKSourceType.local.rawValue
}.first!
do {
try self.eventStore.saveCalendar(userCalendar, commit: true)
print("creating new calendar succesful")
} catch {
print("creating new calendar failed : \(error)")
}
}
如这里所述
还有其他人遇到过这个问题吗?您链接的博客文章在保存日历时做了两件事,它使用
saveCalendar(,commit)
方法将日历保存到事件存储,然后还将日历的标识符保存到用户默认值,以便以后可以检索:
NSUserDefaults.standardUserDefaults().setObject(newCalendar.calendarIdentifier, forKey: "EventTrackerPrimaryCalendar")
您正在执行第一步,而不是第二步,因此您的日历将保留在存储中,但您没有保留将来检索日历所需的信息。您链接的博客文章在保存日历时做两件事,它使用
保存日历(,提交)
方法将日历保存到事件存储,然后还将日历的标识符保存到用户默认值,以便以后检索:
NSUserDefaults.standardUserDefaults().setObject(newCalendar.calendarIdentifier, forKey: "EventTrackerPrimaryCalendar")
您正在执行第一步,但不是第二步,因此您的日历将在存储中持久化,但您没有保留将来检索它们所需的信息。我确实将日历ID保存为UserDefault,但这有点复杂,因此我省略了该代码。正如我所说,我可以向它们写入事件,因此检索它们没有问题。问题是,当我关闭应用程序,然后重新打开它时,即使使用UserDefaults.Hmm中的ID,日历也会被发现为零。tbh我没有使用此API的任何经验,但是如果调用
saveCalendar(,commit:)
时没有发现任何错误,那么我觉得问题在于检索而不是持久性。您是否可以确认您是否有权访问日历(EKEventStore.authorizationStatus(for:)
)返回EKAuthorizationStatus.authorized
?我肯定有权,因为我可以看到我在内置Apple Calendar应用程序中创建的日历以及我在上面创建的所有事件。我知道这是一个持久性问题,因为只有在应用程序在后台放置几秒钟后,日历才会被发现为零。在重新启动我的应用程序之前,我会检查苹果的日历,看看日历和所有的活动都消失了。听起来。。。令人沮丧的:(如果你想要第二双眼睛,我很乐意查看git hub项目。我看到这张便条,有人注意到当iCloud日历启用时,本地日历被隐藏的一些奇怪行为:这可能是一个原因吗?这是一场漫长的战斗,我真的很感谢你的同情和帮助!这篇帖子可能与我的问题类似,beca使用它看起来我们都在将日历保存到(可能)不一致的来源。该项目目前不在gitHub上,但它已在待办事项列表上。我会看看是否可以在某个时候向您发送gitHub链接。我确实将日历ID保存到UserDefaults,但它有点复杂,所以我省略了该代码。正如我所说,我可以向它们写入事件,因此检索它们没有问题。问题是何时我关闭应用程序,然后重新打开它,即使使用UserDefaults.Hmm.tbh中的ID,日历也会被发现为零。我没有使用此API的任何经验,但是如果调用saveCalendar(,commit:)时没有发现任何错误
然后我觉得问题在于检索而不是持久性。您能否确认您是否有访问日历的授权(EKEventStore.authorizationStatus(for:)
)返回EKAuthorizationStatus.authorized
?我肯定有授权,因为我可以看到我在内置的Apple Calendar应用程序中创建的日历以及我在上面创建的所有事件。我知道这是一个持久性问题,因为只有在应用程序在后台放置几秒钟后,日历才被发现为零。Bef或者重新启动我的应用程序,我会检查苹果的日历,看看日历和所有事件都消失了。这听起来…令人沮丧:(如果你想要第二双眼睛,我很乐意查看git hub项目。我看到这张便条,有人注意到当iCloud日历启用时,本地日历被隐藏的一些奇怪行为:这可能是一个原因吗?这是一场漫长的战斗,我真的很感谢你的同情和帮助!这篇帖子可能与我的问题类似,beca使用它似乎我们都在将日历保存到(可能)不一致的源。该项目当前不在gitHub上,但它已在待办事项列表上。我看看是否可以在某个时候为您发送gitHub链接。