我想从本地手机日历加载活动。
以下是代码:
@IBAction func btnpressed() {
let eventStore = EKEventStore()
switch EKEventStore.authorizationStatusForEntityType(EKEntityTypeEvent) {
case .Authorized:
println("autorized")
loadCalendarEvents(eventStore)
case .Denied:
println("Access denied")
case .NotDetermined:
println("not deter")
eventStore.requestAccessToEntityType(EKEntityTypeEvent, completion:
{[weak self] (granted: Bool, error: NSError!) -> Void in
if granted {
println("Access granted")
self!.loadCalendarEvents(eventStore)
} else {
println("Access denied")
}
})
default:
println("Case Default")
}
}
func getDate(string: String) -> NSDate {
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT+0:00")
return dateFormatter.dateFromString(string)!
}
func loadCalendarEvents(store: EKEventStore) {
let calendars = store.calendarsForEntityType(EKEntityTypeEvent) as! [EKCalendar]
let startDate = getDate("2015-08-26")
let endDate = getDate("2015-08-29")
let fetchCalendarEvents: NSPredicate = store.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: calendars)
if let eventList: [EKEvent] = store.eventsMatchingPredicate(fetchCalendarEvents) as? [EKEvent] {
for i in 0..<eventList.count {
var time = "\(eventList[i].startDate) - \(eventList[i].endDate)"
println("\(eventList[i].title) - \(eventList[i].location) - \(time)")
}
} else {
println("no events")
}
}
当我加载事件时,他们的startDate有点不正确。示例:我在“2015-08-26 00:00:00”添加一个事件,但在debuger中显示为“2015-08-25 21:00:00 +0000”。我认为这是因为错误的时区。但我不知道在哪里设置它。以下代码对我不起作用:
for i in 0..<eventList.count {
eventList[i].timeZone = NSTimeZone(abbreviation: "GMT+0:00")
var time = "\(eventList[i].startDate) - \(eventList[i].endDate)"
println("\(eventList[i].title) - \(eventList[i].location) - \(time)")
}
有人可以帮我解决我的问题吗?感谢。