gpt4 book ai didi

Swift 重复本地通知设置

转载 作者:行者123 更新时间:2023-11-30 10:48:17 25 4
gpt4 key购买 nike

我正在尝试快速设置每日重复通知。

我可以使用

设置通知
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?

感谢您的帮助

这就是我拥有 View Controller 的方式。里面viewdidload。然后时间选择器位于另一个 View Controller 内

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}

最佳答案

由于您在另一个 View Controller (第二个)上选择了时间,因此您需要通知第一个(主) View Controller 日期已更改。

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

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

在第二个 Controller 中更新保存功能:

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

同时在主 Controller 中您需要订阅通知:

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}
}

关于Swift 重复本地通知设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55319152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com