[swift] FSCalendar에서 이벤트 알림 시간 설정

FSCalendar은 iOS 앱에서 사용하기 편리한 달력 라이브러리입니다. 이 라이브러리를 사용하여 달력에 이벤트를 표시하고 알림 시간을 설정하는 방법에 대해 알아보겠습니다.

1. FSCalendar 이벤트 표시하기

FSCalendar에서 이벤트를 표시하기 위해서는 FSCalendarDelegate에서 calendar(_:numberOfEventsFor:) 메서드를 구현해야 합니다. 이 메서드는 주어진 날짜에 해당하는 이벤트 수를 반환합니다. 예를 들어, 다음은 일부 날짜에 이벤트가 있는 경우 해당하는 이벤트 수를 반환하는 구현 예시입니다.

func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int {
    // 이벤트가 있는 경우 이벤트 수를 반환합니다.
    if haveEvent(for: date) {
        return 1
    }
    return 0
}

func haveEvent(for date: Date) -> Bool {
    // 해당 날짜에 이벤트가 있는지 확인하고 결과를 반환합니다.
    // 여기서는 임의로 true나 false를 반환합니다.
    return ...
}

위의 예시에서 haveEvent(for:) 메서드는 해당 날짜에 이벤트가 있는지 확인하는 로직을 구현하면 됩니다.

2. 이벤트 알림 시간 설정하기

이벤트 알림 시간을 설정하기 위해서는 UILocalNotification을 사용하여 특정 시간에 알림을 보내야 합니다.

func scheduleEventNotification(date: Date, title: String, message: String) {
    let notification = UILocalNotification()
    notification.alertTitle = title
    notification.alertBody = message
    notification.fireDate = date
    notification.soundName = UILocalNotificationDefaultSoundName
    UIApplication.shared.scheduleLocalNotification(notification)
}

위의 예시에서 scheduleEventNotification(date:title:message:) 메서드는 지정된 시간에 알림을 보내는 기능을 구현한 메서드입니다. 이 메서드를 호출하여 원하는 시간에 알림을 설정할 수 있습니다.

3. FSCalendar과 이벤트 알림 시간 설정 연동하기

FSCalendar에서 이벤트 알림 시간을 설정하기 위해서는 FSCalendarDelegate의 calendar(_:didSelect:) 메서드를 구현하여 사용자가 달력에서 날짜를 선택했을 때 해당 날짜의 이벤트 알림을 설정하도록 구현해야 합니다.

func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
    if haveEvent(for: date) {
        // 원하는 알림 시간 설정하기
        let notificationDate = ...
        
        scheduleEventNotification(date: notificationDate, title: "이벤트 알림", message: "오늘 이벤트가 있습니다!")
    }
}

위의 예시에서는 calendar(_:didSelect:at:) 메서드에서 haveEvent(for:) 메서드로 선택된 날짜에 이벤트가 있는지 확인한 후, 알림 시간을 설정하고 scheduleEventNotification(date:title:message:) 메서드를 호출하여 알림을 예약합니다.

이제 FSCalendar에서 이벤트를 표시하고 알림 시간을 설정하는 방법을 알았습니다. 필요한 경우 이 예시들을 참고하여 적절하게 커스터마이징하여 사용하시면 됩니다.

참고 자료