gpt4 book ai didi

ios - swift 。事件未保存到日历

转载 作者:行者123 更新时间:2023-11-30 11:25:25 26 4
gpt4 key购买 nike

我正在尝试将特定事件从所有日历复制到目标日历。不幸的是,我的事件没有保存在目标日历中。

简单地写一下我的代码:

  • 检查权限(成功)
  • 加载日历(成功)
  • 加载事件(成功)
  • 保存事件(失败)

我确定有一些事件需要从终端保存,该终端会多次打印“尝试保存”。

看起来代码通过“try self.eventStore.save(event, span: .thisEvent)”并退出函数,而不调用“Saved”或输入 catch 子句。

有源代码:

import UIKit
import EventKit

class ViewController: UIViewController{

@IBOutlet weak var status: UILabel!

var calendars: [EKCalendar]?

var targetCalendar: EKCalendar?
var targetCalendarEvents: [EKEvent]?

let eventStore = EKEventStore()

let targetCalendarName = "TargetCalendarName"

override func viewDidLoad() {
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewWillAppear(_ animated: Bool) {
DispatchQueue.main.async{
self.status.text = "Idle"
}

checkCalendarAuthorizationStatus()
}

func checkCalendarAuthorizationStatus() {
let status = EKEventStore.authorizationStatus(for: EKEntityType.event)

switch (status) {
case EKAuthorizationStatus.notDetermined:
// This happens on first-run
requestAccessToCalendar()
case EKAuthorizationStatus.authorized:
// Things are in line with being able to show the calendars in the table view
loadCalendars()
loadEvents()
case EKAuthorizationStatus.restricted, EKAuthorizationStatus.denied:
// We need to help them give us permission
print("Missing permissions [00]")
}
}

func requestAccessToCalendar() {
eventStore.requestAccess(to: EKEntityType.event, completion: {
(accessGranted: Bool, error: Error?) in

if accessGranted == true {
DispatchQueue.main.async(execute: {
self.loadCalendars()
self.loadEvents()
})
} else {
print("Missing permissions [01]")
}
})
}

func loadEvents(){
print("Loading..")
DispatchQueue.main.async{
self.status.text = "Loading"
}

let eventStore = EKEventStore()

var initialized = false

//Two months
let dateTo = NSDate(timeIntervalSinceNow: +30*24*3600 * 2)

for calendar in self.calendars! {
let predicate = eventStore.predicateForEvents(withStart: Date() as Date as Date, end: dateTo as Date, calendars: [calendar])

let events = eventStore.events(matching: predicate)

if calendar.title == targetCalendarName {
print("Initialized")
targetCalendar = calendar
targetCalendarEvents = events
initialized = true
}
}

if(!initialized){
print("Not Initialized")
} else {
for calendar in self.calendars! {

let predicate = eventStore.predicateForEvents(withStart: Date() as Date as Date, end: dateTo as Date, calendars: [calendar])

let events = eventStore.events(matching: predicate)

if calendar.title != targetCalendarName && calendar.title != "Contacts" {

//print("Loaded Calendar \(calendar.title)")
print("Loaded Calendar")

for event in events {
if(!(event.location?.isEmpty)!){
//print("Event \(event.location ?? "Missing Location") \(event.startDate) \(event.endDate)")

addEventToTarget(eventToAdd: event)
}
}
}
}
}


DispatchQueue.main.async {
self.status.text = "Done"
}
print("Done")
}

func loadCalendars() {
self.calendars = eventStore.calendars(for: EKEntityType.event)
}

func addEventToTarget(eventToAdd: EKEvent){

eventStore.requestAccess(to: .event) { (granted, error) in

for event in self.targetCalendarEvents! {
if(!(event.location?.isEmpty)!){
if(
eventToAdd.title == event.title &&
eventToAdd.startDate == event.startDate &&
eventToAdd.endDate == event.endDate
){
print("Possible duplicate - skipping")
return
}
}
}

if (granted) && (error == nil) {

let event:EKEvent = EKEvent(eventStore: self.eventStore)

event.title = eventToAdd.title
event.startDate = eventToAdd.startDate
event.endDate = eventToAdd.endDate
event.notes = ""
event.location = eventToAdd.location

event.calendar = self.targetCalendar

//print("Trying to save \(event.title) \(String(describing: event.location))")
print("Trying to save")
do {
try self.eventStore.save(event, span: .thisEvent)
print("Saved \(event.title) \(String(describing: event.location)) in \(event.calendar.title)")
} catch {
print("failed to save event with error : \(error as NSError)")
}
}
else{

print("failed to save event with error : \(String(describing: error)) or access not granted")
}
}
}
}

TargetCalendarName 是正确的日历名称

<小时/>

有时,有些事件可以成功保存,但只有其中几个(2-5),数百个应该保存。我不知道为什么。所以 200 block 5 对我来说还不够。

最佳答案

感谢@matt 提出解决方案。

现在我只保存事件一次(之前我请求访问每个事件,这是非常糟糕的),因此我请求访问 eventStore 一次并且可以成功保存事件。

func saveEvents(){
eventStore.requestAccess(to: .event) { (granted, error) in

if (granted) && (error == nil) {

for event in self.eventsToCopy {
print("Trying to save")
do {
try self.eventStore.save(event, span: .thisEvent)
print("Saved \(event.title) \(String(describing: event.location)) in \(event.calendar.title)")
} catch {
print("failed to save event with error : \(error as NSError)")
}
}
}
else{

print("failed to save event with error : \(String(describing: error)) or access not granted")
}
}
}

关于ios - swift 。事件未保存到日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50788551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com