我有本方法允许在EventKit中创建日历事件。
func createEvent(eventStore: EKEventStore, title: String, startDate: NSDate, endDate: NSDate) {
let event = EKEvent(eventStore: eventStore)
event.title = title
event.startDate = startDate
event.endDate = endDate
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.saveEvent(event, span: .ThisEvent)
savedEventId = event.eventIdentifier
} catch {
print("Bad things happened")
}
}
但是,我希望允许用户选择一周中的几天来创建重复活动。例如,用户设置上午10:05的时间,并选择星期一,星期三,星期四。如何制定EventKit以允许此事件在用户日历上正确安排?
答案 0 :(得分:2)
我发现您的帖子上的时间戳可以追溯到5月,所以您可能不再需要帮助了,但对于遇到此问题的其他任何人,您必须使用EKRecurrenceRule class和将它附加到活动中。
这是Objective-C中的一个示例,用它来为每个星期五安排一个事件(就像你的场景一样):
// The EKRecurrenceRule accepts an NSArray of day objects, I did this to make adding multiple days easier
NSMutableArray *daysArr = [[NSMutableArray alloc] init];
// Days of the week are ordered Sunday to Saturday, 1-7
EKRecurrenceDayOfWeek *theDay = [[EKRecurrenceDayOfWeek alloc] initWithDayOfTheWeek:6 weekNumber:0];
// Add Friday to the array
[daysArr addObject:theDay];
// Instantiate the EKRecurrenceRule, giving it a weekly frequency and the day of the week
EKRecurrenceRule *recurrenceRule = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyWeekly interval:1 daysOfTheWeek:daysArr daysOfTheMonth:nil monthsOfTheYear:nil weeksOfTheYear:nil daysOfTheYear:nil setPositions:nil end:nil];
// Apply it to the event
[event setRecurrenceRules:@[recurrenceRule]];
我希望这可以帮助别人!
答案 1 :(得分:0)
我知道这可能也会迟到,但至少它适用于Swift而不是ObjC。我创建了两个函数,一个在底部创建Date事件,另一个创建名为addEventToCalendar的重复事件。请注意,您仍需要澄清特定事件的星期几。
如果您对事件的运作方式仍有疑问,建议您咨询this link。
func addEventToCalendar(title: String, description: String?, startDate: Date, endDate: Date, completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil){
let eventStore = EKEventStore()
eventStore.requestAccess(to: .event, completion: { (granted, error) in
if (granted) && (error == nil) {
let event = EKEvent(eventStore: eventStore)
event.title = title
event.startDate = startDate
event.endDate = startDate
event.notes = description
event.calendar = eventStore.defaultCalendarForNewEvents
let recurrenceRule = EKRecurrenceRule.init(
recurrenceWith: .daily,
interval: 1,
daysOfTheWeek: [EKRecurrenceDayOfWeek.init(EKWeekday.saturday)],
daysOfTheMonth: nil,
monthsOfTheYear: nil,
weeksOfTheYear: nil,
daysOfTheYear: nil,
setPositions: nil,
end: EKRecurrenceEnd.init(end:endDate)
)
event.recurrenceRules = [recurrenceRule]
do {
try eventStore.save(event, span: .thisEvent)
} catch let e as NSError {
completion?(false, e)
return
}
completion?(true, nil)
} else {
completion?(false, error as NSError?)
}
})
}
private func createDate(date: String!, hours: Int!, minutes:Int! ) -> Date{
var fullDateArr = date.components(separatedBy: "-")
let startMonth: Int? = Int(fullDateArr[0])!
let startDay: Int? = Int(fullDateArr[1])!
let startYear: Int = Int(fullDateArr[2])!
var dateComponents = DateComponents()
dateComponents.year = startYear
dateComponents.month = startMonth
dateComponents.day = startDay
dateComponents.timeZone = TimeZone(abbreviation: "MDT") // ELP Standard Time
dateComponents.hour = hours
dateComponents.minute = minutes
let userCalendar = Calendar.current // user calendar
let someDateTime = userCalendar.date(from: dateComponents)
return someDateTime!
}