在 Swift 3 中从日期选择器安排每周可重复的本地通知和触发日期

Scheduling weekly repeatable local notification with fire date from date picker in Swift 3

所以我目前正在构建一个日程应用程序,并且我正在尝试创建一个本地通知以在每周的特定日期的特定时间触发。所以我做的第一件事是获取事件开始时间的日期值,然后我从开始时间值中减去 5 分钟,然后安排通知。以前很容易只需要输入: notification.repeatInterval = CalendarUnit.WeekOfYear 但现在该命令在 Swift 3 中已弃用,是的,所以我找到的唯一方法是:

 let someMinutesEarlier = Calendar.current.date(byAdding: .minute, value: -5, to: startTimePicker.date)

        let contentOfNotification = UNMutableNotificationContent()

        let interval = someMinutesEarlier?.timeIntervalSinceNow

        contentOfNotification.title = "Event starting"
        contentOfNotification.body = "Some notes"
        contentOfNotification.sound = UNNotificationSound.default()

        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval!, repeats: true)
        let request = UNNotificationRequest.init(identifier: notificationIdentifier, content: contentOfNotification, trigger: trigger)

        let center = UNUserNotificationCenter.current()
        center.add(request) { (error) in
            print(error as Any)
        }

但这只安排了一次通知(无论重复布尔值设置为真),因为年份在 someMinutesEarlier 中......或者它可能是其他东西?有什么想法吗?

正如 McNight 提到的,您可以像这样使用 UNCalendarNotificationTrigger

let interval = 60 * 60 * 24 * 7 - 300 // One week minus 5 minutes.
let alarmTime = Calendar.current.date(byAdding: .second, value: interval, to: Date())!
let components = Calendar.current.dateComponents([.weekday, .hour, .minute], from: alarmTime)
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)

(我没有测试过这段代码,但这应该会让你走上正确的道路)。

更多信息here

编辑: SwiftyCruz 建议的固定时间间隔计算。

编辑 2:更新为使用日历执行 RickiG 建议的时移。

// Swift2.3

func setLNotification(weekDay:Int , hour:Int, min:Int, second:Int, alertBody:String, type:String, isRepeate:Bool){

    let calender = NSCalendar(identifier: NSCalendarIdentifierGregorian)
    let dateComp: NSDateComponents?

    dateComp = calender?.components([.Year,.WeekOfMonth,.Month], fromDate: NSDate())
    dateComp?.hour = hour
    dateComp?.minute = min
    dateComp?.second = 00
    dateComp?.weekday = weekDay
    dateComp!.timeZone = NSTimeZone.localTimeZone()

    print(calender?.dateFromComponents(dateComp!))


    let SetCustomDate = calender?.dateFromComponents(dateComp!)

    print(SetCustomDate)

    let notification = UILocalNotification()
    if isRepeate == true{

        switch type {
        case "Weekly":

            notification.fireDate = SetCustomDate!.dateByAddingTimeInterval(60*60*24*7)
            notification.repeatInterval = NSCalendarUnit.Weekday

        case "2 Weekly":

            notification.fireDate = SetCustomDate!.dateByAddingTimeInterval(60*60*24*14)
            notification.repeatInterval = NSCalendarUnit.Day
        case "Monthly":
            notification.fireDate = SetCustomDate!.dateByAddingTimeInterval(60*60*24*28)
            notification.repeatInterval = NSCalendarUnit.Day

        default:
            break;
        }

        notification.soundName = UILocalNotificationDefaultSoundName
        notification.repeatCalendar = calender
    }
    notification.alertTitle = "STATS"
    notification.alertBody = "Please update your Stats detail"
    notification.userInfo = ["uid":"reminder"]

    print(notification)

   UIApplication.sharedApplication().scheduleLocalNotification(notification)



}