Repeat UILocalNotification on Specific day

后端 未结 5 1491
滥情空心
滥情空心 2021-02-03 14:30

I need to set UILocalNotification, I just need to take hour and minute from DatePicker and need to set specific date ( like : Monday ) and repeat it on every Monday.

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-03 15:04

    Swift 3:

    func calculateSpecificDay(_ desiredWeekday: NSInteger, fromDate: Date = Date()) -> Date {
    
        let weekDateRange = NSCalendar.current.maximumRange(of: .weekday)!
        let daysInWeek = weekDateRange.lowerBound.distance(to: weekDateRange.upperBound) - weekDateRange.lowerBound + 1
    
        let currentWeekday = NSCalendar.current.dateComponents([.weekday], from: fromDate).weekday
        let differenceDays = (desiredWeekday - currentWeekday! + daysInWeek) % daysInWeek
    
        var dateComponents = DateComponents()
        dateComponents.day = differenceDays
    
        let calculatedFridayDate = NSCalendar.current.date(byAdding: dateComponents, to: fromDate)
        return calculatedFridayDate!
    }
    
    If selected day is sunday then pass 1
    If selected day is monday then pass 2
    .
    .
    .
    If selected day is saturday then pass 7
    

提交回复
热议问题