问题描述
我正在使用Swift 4.1.我想编写一个函数,该函数将从iOS的日历"应用中的所有日历中收集所有事件.感谢关于stackoverflow的以下回答:如何获取所有事件在日历之外(Swift),我能够编写自己的 class
并将其命名为 Cale
.请看这个:
I am using Swift 4.1. And I want to write a function which will collect all events from all calendars in Calendar app of iOS. Thanks to this answer on stackoverflow: How to get all Events out of a Calendar (Swift) I was able to write my own class
and call it Cale
. Please, look at this:
import UIKit
import EventKit
class Cale {
private func createDate(year: Int) -> Date {
var components = DateComponents()
components.year = year
components.timeZone = TimeZone(secondsFromGMT: 0)
return Calendar.current.date(from: components)!
}
private let eventStore = EKEventStore()
private func get() {
let calendars = eventStore.calendars(for: .event)
for calendar in calendars {
// This checking will remove Birthdays and Hollidays callendars
guard calendar.allowsContentModifications else {
continue
}
let start = createDate(year: 2016)
let end = createDate(year: 2025)
print("start: \(start)")
print(" end: \(end)")
let predicate = eventStore.predicateForEvents(withStart: start, end: end, calendars: [calendar])
print("predicate: \(predicate)")
let events = eventStore.events(matching: predicate)
for event in events {
print(" title: \(event.title!)")
print("startDate: \(event.startDate!)")
print(" endDate: \(event.endDate!)")
}
}
}
func checkStatusAndGetAllEvents() {
let currentStatus = EKEventStore.authorizationStatus(for: EKEntityType.event)
switch currentStatus {
case .authorized:
//print("authorized")
self.get()
case .notDetermined:
//print("notDetermined")
eventStore.requestAccess(to: .event) { accessGranted, error in
if accessGranted {
self.get()
} else {
print("Change Settings to Allow Access")
}
}
case .restricted:
print("restricted")
case .denied:
print("denied")
}
}
}
非常简单的类,您可以使用它,它可以工作,但有一个例外.其中的主要功能是 get()
.在此功能中,我基于两个日期创建谓词:开始和结束.如您所见,开始日期是:
Quite simple class, you can use it, it is working but with one exception.The main function there is get()
In this function I am creating predicate based on two dates: start and end. As you can see start date is:
,结束日期为:
但是,如果我们运行该程序,我们将看到谓词将如下所示:
But if we run the program we will see that predicate will be like this:
仅从2016年到2020年的4年!我已经在不同的日期对其进行了测试,但是我可以得出最大间隔为4年的谓词.这意味着,它不会给我所有事件!所以问题是:如何从日历中获取所有事件?如果可能的话,不使用日期!
Only from 2016 to 2020, 4 years! I have tested it on different dates, but I could get predicate with 4 years interval maximum. It means, it will not give me all events! So question is: How to get all events from calendar? If it possible, without using dates!
感谢您将来的帮助或建议!
Thank you for any future help or advice!
推荐答案
如果有人仍在怀疑,Apple特意将此期限限制为四年.
In case anyone is still wondering, Apple limited this to four years intentionally.
来自 predicateForEvents(withStart:end:calendars :) :
如果您想要一个更大的范围,我想您必须将其包装在自己的函数中,以将其分为四年.
If you want a range longer than that, I guess you'll have to wrap this in your own function to split it into four year chunks.
这篇关于Swift 4如何从日历中获取所有事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!