重新运行应用后,在UIDatePicker中显示节省的时间

时间:2018-11-27 12:06:47

标签: ios swift datepicker uidatepicker

我可以通过UIDatePicker设置通知时间。但是,如果我关闭该应用程序并再次运行,那么时间将重置为当前时间。尽管通知本身将在先前规定的时间内显示。如何在之前保存的UIDatePicker(timeDatePicker和timeDatePickerSecond)中显示时间(单击“计划”后)?

enter image description here

import UIKit
import UserNotifications

class Notifications: UIViewController, UNUserNotificationCenterDelegate {

    @IBOutlet weak var scheludeMeal: UIButton!
    @IBOutlet weak var timeDatePicker: UIDatePicker!
    @IBOutlet weak var timeDatePickerSecond: UIDatePicker!


    override func viewDidLoad() {
        super.viewDidLoad()

        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
            if granted {
                print("Yes!")
            } else {
                print("No...?")
            }
        }

    }


    @IBAction func scheludeButton(_ sender: Any) {

        let center = UNUserNotificationCenter.current()

        center.removeAllPendingNotificationRequests()

        let content = UNMutableNotificationContent()
        content.title = "Notification!"
        content.body = "Description of notification :)"
        content.sound = UNNotificationSound.default

        let dateComponent = timeDatePicker.calendar.dateComponents([.hour, .minute], from: timeDatePicker.date)
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: true)
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        center.add(request)

        let dateComponentSd = timeDatePickerSecond.calendar.dateComponents([.hour, .minute], from: timeDatePickerSecond.date)
        let trigger2 = UNCalendarNotificationTrigger(dateMatching: dateComponentSd, repeats: true)
        let request2 = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger2)
        center.add(request2)

    }

}

1 个答案:

答案 0 :(得分:2)

您可以使用UserDefaults在应用程序启动之间持久保存Date对象。您只需要确保将新选择的Date对象保存在scheduleButton中,并从UserDefaults中的viewDidLoad加载值以将其设置为默认日期选择器。

您可以找到为timeDatePicker实现的上述解决方案,但是如果timeDatePickerSecond也需要该解决方案,只需为其创建另一个Date?属性,然后同样实施,即可为该第二个属性使用不同的UserDefaults键。

import UIKit
import UserNotifications

class Notifications: UIViewController, UNUserNotificationCenterDelegate {

    @IBOutlet weak var scheludeMeal: UIButton!
    @IBOutlet weak var timeDatePicker: UIDatePicker!
    @IBOutlet weak var timeDatePickerSecond: UIDatePicker!

    var savedNotificationDate: Date? {
        get {
            return UserDefaults.standard.object(forKey: "notificationDate") as? Date
        }
        set {
            UserDefaults.standard.set(newValue, forKey: "notificationDate")
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        if let savedNotificationDate = savedNotificationDate {
            timeDatePicker.date = savedNotificationDate
        }

        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
            if granted {
                print("Yes!")
            } else {
                print("No...?")
            }
        }

    }


    @IBAction func scheludeButton(_ sender: Any) {

        let center = UNUserNotificationCenter.current()

        center.removeAllPendingNotificationRequests()

        let content = UNMutableNotificationContent()
        content.title = "Notification!"
        content.body = "Description of notification :)"
        content.sound = UNNotificationSound.default

        let dateComponent = timeDatePicker.calendar.dateComponents([.hour, .minute], from: timeDatePicker.date)
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: true)
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        center.add(request)
        savedNotificationDate = timeDatePicker.date

        let dateComponentSd = timeDatePickerSecond.calendar.dateComponents([.hour, .minute], from: timeDatePickerSecond.date)
        let trigger2 = UNCalendarNotificationTrigger(dateMatching: dateComponentSd, repeats: true)
        let request2 = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger2)
        center.add(request2)

    }

}