我正在尝试调用一种在创建事件时获取日历的方法。我确定这个方法是使用calendarWithIdentifier来获取
的ID这是我创建事件的代码,并调用将获取日历的方法。我需要拨打特定的日历
var store = EKEventStore()
func applicationDidFinishLaunching(aNotification: NSNotification) {
// ask for permission
// call methods
}
// Create new calendar
func createCalendar() -> EKCalendar? {
var calendar = EKCalendar(forEntityType: EKEntityTypeEvent, eventStore: self.store)
calendar.title = "Calendarname"
// ...
return calendar
}
func createEvent(){
var newEvent : EKEvent = EKEvent(eventStore: store)
var error : NSError? = nil
newEvent.title = "day off"
newEvent.location = "London"
var startDate : String = "2015-07-16";
var dateFmt1 = NSDateFormatter()
dateFmt1.dateFormat = "yyyy-MM-dd"
var date1:NSDate = dateFmt1.dateFromString(startDate)!;
var endDate : String = "2015-07-17";
var dateFmt2 = NSDateFormatter()
dateFmt2.dateFormat = "yyyy-MM-dd"
var date2:NSDate = dateFmt2.dateFromString(endDate)!;
newEvent.startDate = date1
newEvent.endDate = date2
newEvent.notes = "testing the event"
newEvent.calendar = getCalendar()
self.store.saveEvent(newEvent, span: EKSpanThisEvent, commit: true, error: nil)
}
func getCalendar() -> EKCalendar? {
// ...
}
我知道这个
func calendarWithIdentifier(_ identifier: String!) -> EKCalendar!
来自Apple的EventKit文档,用于按ID返回日历。所以我使用Calendar的名称(“Calendarname”)作为参数,但这似乎不对。它回来了。
有人可以帮我正确地获取日历吗?谢谢
答案 0 :(得分:4)
方法calendarWithIdentifier
需要一个UID而不是一个可读的名称。您需要从store
这样检索它们:
var calUid:String = "?"
for cal in store.calendarsForEntityType(EKEntityTypeEvent) {
if cal.title == "Calendarname" { calUid = cal.calendarIdentifier }
}
以后你可以像
一样使用它calendar = store.calendarWithIdentifier(calUid)
答案 1 :(得分:1)
我能够让它运转起来。这里是getCalendar函数:
func getPSACalendar() -> EKCalendar{
var calUID:String = "?"
var cal = store.calendarsForEntityType(EKEntityTypeEvent) as [EKCalendar]
for i in cal {
if i.title == "Calendarname" {
calUID = i.calendarIdentifier
}
}
var calendar = store.calendarWithIdentifier(calUID)
return calendar
}