作者:mizrke | 来源:互联网 | 2024-10-13 08:00
我正在尝试获取日历事件和提醒,但有些却不返回事件,提醒,日历,事件源的列表。我已获得访问事件的权限,但即使获得许可,我也无法获取数据。但是,如果我去设置并尝试打开和关闭日历权限,那么我将成功获取所有数据。以下是我发送的代码,如果我输入错误,请提出建议。
func requestaccessToCalendar() {
self.eventStore.requestaccess(to: EKEntityType.event) { (accessGranted,error) in
if accessGranted == true {
DispatchQueue.main.async(execute: {
self.isAuthorized = true
let application = UIApplication.shared
application.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
self.authorizationCompletionHandler!(true,.authorized)
})
} else {
DispatchQueue.main.async(execute: {
self.isAuthorized = false
self.authorizationCompletionHandler!(false,.denied)
})
}
}
}
let status = EKEventStore.authorizationStatus(for: EKEntityType.event)
if status == .authorized {
// let startDate = Date()
// let endDate = Date().dateSixtyDaysAfter()
let startDate = self.calendarDates().0//It return me the date of two months ago
let endDate = self.calendarDates().1 // Return todays date.
let calendars = self.eventStore.calendars(for: .event)
print("calendars: \(calendars)")//Here i get empty list of calendar even i have 26 calendar in device.
let eventsPredicate = self.eventStore.predicateForEvents(withStart: startDate!,end: endDate!,calendars: nil)
self.events = self.eventStore.events(matching: eventsPredicate)
var eventInfoList = [[String: AnyObject]]()
if let events = self.events {
for event in events {
//Here i did some operation on calendar event
}
} else {
print("No calendar and data found")
}
} else {
print("Don't have calendar access")
}