设置事件的重现次数"每个月重复一次"而不是"每个月重复一次,每次31个" IOS

时间:2017-06-19 08:42:33

标签: ios swift events ekevent ekrecurrencerule

  1. 我正在设置一个事件,比如某个月的第13个月,并且每个月重复一次。它每个月都会重复正常。 但是当它设置为某个月的第31个月时,它将被设置为仅有31个月的数月,并且如果它显示在Edit-> Repeat->每个月重复一次,每个31个。
  2. 如果当月没有可用的第31天,我希望它至少设定为最后一个月。
  3. 同样在2月29日,30日或31日,也不会创建具有这些日期的其他月份的活动。
  4. 请注意,我们希望设置连续的事件集,以便轻松删除和编辑它们。

    我写了以下代码:

    let eventstore : EKEventStore = EKEventStore()
            eventstore.requestAccess(to: EKEntityType.event, completion: { (isallow, error) -> Void in
                var event:EKEvent!
    
                if (event == nil) {
                    event = EKEvent(eventStore: eventstore)
                    event.title = self.getReminderTitleForCalendarEvent(dtDate: date)
                    event.notes = ""
                    event.startDate = date
                    event.endDate = date
                    event.calendar = eventstore.defaultCalendarForNewEvents
                    let ekrules: EKRecurrenceRule = EKRecurrenceRule.init(recurrenceWith: EKRecurrenceFrequency.monthly, interval:self.selectedFrequency!, end: nil)
                    event.recurrenceRules = [ekrules]
                }
                else // if there is already a event then update date
                {
    
                    // if event is deleted by user
                    event.startDate = date
                    event.endDate = date
                    if let oldalarm = event.alarms?[0]// remove old date alarm
                    {
                        event.removeAlarm(oldalarm)
                    }
                    // Below code is to remove existing event
                    do
                    {
                        try  eventstore.remove(event, span: .thisEvent)
                        //self.objselectedinfusion.strEventId = event.eventIdentifier
                    }
                    catch let error as NSError {
                        //print(error.localizedDescription)
                    }
                }
                let alarm = EKAlarm(relativeOffset: self.getTimeIntervalForAlert())
    
       let alarm = EKAlarm(absoluteDate: reminderFireDate as Date)
      event.addAlarm(alarm)
    
                do
                {
                    try eventstore.save(event, span: EKSpan.futureEvents, commit: true)
                    UserDefaults.standard.set(event.eventIdentifier, forKey: Constant.calendarEventID)
                }
                catch let error as NSError {
                    //print(error.localizedDescription)
                }
    
            })
    

    此处selectedFrequency是重复间隔,即几个月。

1 个答案:

答案 0 :(得分:0)

 class  func getRepeatValue (_ option : String) -> EKRecurrenceRule?{

    // ["Daily" , "Weekly" , "Monthly" ,"Yearly","None"]

    //        "daily" , "weekly" , "monthly" ,"yearly","none"

    switch option {
    case "Daily":


        let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.daily, interval: 1, end: nil)

        //daily for 50 years
        return rule
    case "Weekly":

        //on the same week day for 50 years
        let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.weekly, interval: 1, end: nil)


        return rule
    case "Monthly":

        //on the same date of every month
        let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.monthly, interval: 1, end: nil)


        return rule

    case "Yearly":

        //on the same date and month of the year
        let rule = EKRecurrenceRule(recurrenceWith: EKRecurrenceFrequency.yearly, interval: 1, end: nil)


        return rule
    case "None":
        return nil
    default:
        return nil
    }
}