将EKEvent保存到特定的日历

时间:2019-03-05 22:31:39

标签: ios ekevent ekcalendar

我正在开发一个需要创建自定义日历事件的应用程序。我已经写了下面的代码,但似乎在主线程上运行还是在辅助线程上运行存在问题-我认为。基本问题始终是不使用EKEventKitUI(我正在创建自己的界面)如何将事件保存到特定的日历。

我是一名新开发人员-因此,我们将不胜感激。

import UIKit
import EventKit

class createEventVC: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource  {

@IBAction func saveDateAction(_ sender: Any) {
let eventStore: EKEventStore = EKEventStore()
eventStore.requestAccess(to: .event) {(granted, error) in
 if (granted) && (error == nil)
 {
 let event: EKEvent = EKEvent(eventStore: eventStore)
 event.title = self.eventtitle
 event.isAllDay = true
 var row: Double = 0
 DispatchQueue.main.async {
   event.startDate = self.DatePicker.date
   event.endDate = self.DatePicker.date
   event.notes = self.AlertNotices.text
   row = Double(self.DaysPicker.selectedRow(inComponent: 0))
   let reloffset: Double = 60 * 60 * 24 * (row+1.5)
   let alarm: EKAlarm = EKAlarm(relativeOffset: -reloffset)
   event.addAlarm(alarm)
   event.calendar = self.getMYCalendar()
  }
  do {
   try eventStore.save(event, span: .thisEvent, commit: true)
  } catch let error as NSError {
   print(error)}
  }
 } else {
   DispatchQueue.main.async {
    self.calAccessDeniedError()}
 }
 }
}

func getMYCalendar() -> EKCalendar {
  let eventStore : EKEventStore = EKEventStore()
  let allcals: [EKCalendar] = eventStore.calendars(for: .event)
  var MYcal: EKCalendar = eventStore.defaultCalendarForNewEvents!
  for i in 0...(allcals.count-1) {
     if allcals[i].title == "MINE"{
        MYcal = allcals[i]
        return MYcal
     }
  }
  return MYcal
}

0 个答案:

没有答案