我之前在defaultCalendarForNewEvents
保存了我的日历活动,它正在运行。今天它不再起作用,但给出错误代码:6“日历是只读的”。
在尝试修改日历之前,请求权限正确。为什么它不再起作用,为什么它在使用相同的代码之前工作?
注意:我正在使用XCode的iPhone模拟器来测试它。
代码 :
func createEvent(eventStore: EKEventStore, title: String, startDate: NSDate, endDate: NSDate) {
let event = EKEvent(eventStore: eventStore)
event.title = "Dodgy Codes"
event.startDate = startDate
event.endDate = endDate
event.calendar = eventStore.defaultCalendarForNewEvents
//Hardcoded testing data
event.location = "Amsterdam"
event.notes = "notes notes notes notes"
do {
try eventStore.saveEvent(event, span: .ThisEvent)
} catch let error as NSError {
//error: Calendar is read only
print("\(NSStringFromClass(CalendarController)): Adding event: \(error.localizedDescription)")
}
}
答案 0 :(得分:0)
我认为这是授权问题,您需要获得访问日历的权限,请尝试这样我希望它能为您服务
func checkForEventAuthorization(eventStore: EKEventStore, completionHandler block:(granted: Bool, error: NSError?) -> Void) {
if eventStore.respondsToSelector(Selector("requestAccessToEntityType:completion:")) == true {
eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (granted, error) -> Void in
block(granted: granted, error: error)
})
} else {
block (granted: true, error: nil)
}
}
func createEvent(eventStore: EKEventStore, title: String, startDate: NSDate, endDate: NSDate) {
self.checkForEventAuthorization(eventStore, completionHandler: { (granted, error) -> Void in
if granted == true {
let event = EKEvent(eventStore: eventStore)
event.title = "Dodgy Codes"
event.startDate = startDate
event.endDate = endDate
event.calendar = eventStore.defaultCalendarForNewEvents
//Hardcoded testing data
event.location = "Amsterdam"
event.notes = "notes notes notes notes"
do {
try eventStore.saveEvent(event, span: .ThisEvent)
} catch let error as NSError {
//error: Calendar is read only
print("\(NSStringFromClass(CalendarController)): Adding event: \(error.localizedDescription)")
}
}
})
}
答案 1 :(得分:0)
好的,好像这个问题已经解决了,因为它首先不存在。我混淆了自己在模拟器上运行它,而不是真正的设备。试图在iPhone6上运行相同的代码。在模拟器上它没有! 对初学者的建议:iOS模拟器并不总是可靠的,在达到一定程度的复杂性后,它变得非常不可靠,尤其是与EKEventStore等服务相关的问题。更好地在真实设备上测试它,伙计们。