Search code examples
iosjsonswift4xcode9fscalendar

Display Events from API in FSCalendar


This is my response from api

SUCCESS: { 
  "5": {
  "count": 1,
  "topEvent": false
  },
  "6": {
  "count": 1,
  "topEvent": false
  },
  "7": {
  "count": 3,
  "topEvent": true
  },
  "success": 1
}

In this response i have events in day 5,6 & 7, rest of the days there are no events. Now, I would need to show the count in the center of calendar icon and it should be green when the topEvent is true.

what logic i apply in

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {

}

please help me..


Solution

  • If you have date parameter then To Set Events Base On Dates. Instance of DateFormatter And Variables:

    var datesWithEvent = ["2015-10-03", "2015-10-06", "2015-10-12", "2015-10-25"]
    
    var datesWithMultipleEvents = ["2015-10-08", "2015-10-16", "2015-10-20", "2015-10-28"]
    
    fileprivate lazy var dateFormatter2: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter
    }()
    
    func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
    
        let dateString = self.dateFormatter2.string(from: date)
    
        if self.datesWithEvent.contains(dateString) {
            return 1
        }
    
        if self.datesWithMultipleEvents.contains(dateString) {
            return 3
        }
    
        return 0
    }
    

    for highlight Event

    func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, eventDefaultColorsFor date: Date) -> [UIColor]? {
            let key = self.dateFormatter2.string(from: date)
            if (self.remainderDates.contains(key)) {
                return [UIColor.red]
            }
            return nil
        }