【问题标题】:Swift repeated local notification set upSwift重复本地通知设置
【发布时间】:2023-04-02 00:11:02
【问题描述】:

我正在尝试使用 swift 设置每日重复通知。

我可以使用

设置通知
var dateComponents = DateComponents()
dateComponents.hour = 17
dateComponents.minute = 00

但我希望用户能够更改它,我设置了一个日期选择器并将其更改为仅时间

在日期选择器中,我可以通过执行以下操作来保存它

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    loadDate(animation: true)
}

func saveDate() {
    UserDefaults.standard.set(TimePicker.date, forKey:dateKey)
}

func loadDate(animation: Bool) {
    guard let loadedDate = UserDefaults.standard.object(forKey: dateKey) as? NSDate else { return }

    TimePicker.setDate(loadedDate as Date, animated: animation)
}

如何将小时和分钟选择的内容传递到通知时间?另外,如果他们选择下午 5 点,我将如何将时间转换为 24 小时,它会将日期组件设置为 17?

感谢您的帮助

这就是我拥有视图控制器的方式。在 viewdidload 里面。然后时间选择器在另一个视图控制器中

let center = UNUserNotificationCenter.current()

center.requestAuthorization(options: [.alert, .sound ]){(grandted,error)in }


let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body"

var dateComponents = DateComponents()
dateComponents.hour = 17
dateComponents.minute = 18

let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)


let uuidstring = UUID().uuidString

let request = UNNotificationRequest(identifier: uuidstring, content: content, trigger: trigger)

center.add(request) {(error) in}

【问题讨论】:

    标签: swift xcode uidatepicker timepicker localnotification


    【解决方案1】:

    由于您在另一个视图控制器(第二个)上进行时间选择,因此您需要通知第一个(主)视图控制器日期已更改。

    这里有很多不同的方法来做到这一点。 Passing Data between View Controllers
    选择任何你喜欢的,在这个例子中我将使用通知:

    extension Notification.Name {
        /// Notification used to pass user selected date
        static let dateChanged = Notification.Name(rawValue: "DateChanged")
    }
    

    在第二个控制器更新保存功能中:

    func saveDate() {
        UserDefaults.standard.set(TimePicker.date, forKey:dateKey)
        /// Send notification with new time
        NotificationCenter.default.post(name: .dateChanged, object: TimePicker.date)
    }
    

    同时在主控制器中你需要订阅通知:

    deinit {
        /// We need to unsubscribe from notifications, otherwise app can crash
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        /// Subscribe to notifications 
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(dateChanged(_:)),
            name: .dateChanged,
            object: nil
        )
    }
    
    /// This method will be called when controller receive notification
    /// It need to be annotated with @objc
    @objc func dateChanged(_ notification: Notification) {
        let date = notification.object as! Date
        let components = Calendar.current.dateComponents([.hour, .minute], from: date)
        setNotification(with: components)
    }
    
    /// By always using same ID for local notifications 
    /// You can easily remove them when time changes
    private static let notificationID = "MyAppNotificationID"
    
    func setNotification(with components: DateComponents) {
        /// Remove previously planned notification
        center.removePendingNotificationRequests(withIdentifiers: [MainVC.notificationID])
    
        /// Create new notificaion
        let content = UNMutableNotificationContent()
        content.title = "Title"
        content.body = "Body"
        let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
        let request = UNNotificationRequest(identifier: MainVC.notificationID, content: content, trigger: trigger)
    
        /// Add to center
        center.add(request) {(error) in}
    }
    

    【讨论】:

    • 似乎无法让它像那样工作,对于触发器,我仍然将它作为 dateComponents 我将如何将其替换为用户时间选择器?非常感谢您的帮助
    • @YT32Z 请展示您如何创建通知请求
    • @ManWinBear。我编辑了帖子并展示了通知是如何设置的。我将它放在主视图控制器中,然后将日期选择器放在另一个视图控制器中。如果需要,我可以将它们放在同一个中,但如果我可以从另一个视图调用该函数,它可能会更好。我对 swift 真的很陌生,所以我不确定如何去做。这也会保持默认时间吗?再次感谢
    • @ManWinBear 非常感谢您的帮助,我非常感谢它完美运行!你是男人
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多