gpt4 book ai didi

ios - 如何按小时和日期显示本地议程或本地通知

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

我有这个数组,它已经填充在带有动态单元格的表格 View 中,我想要它创建一个本地议程或通知或警报或类似的东西,以提醒用户在显示的日期和时间吃药在数组中,你能帮我吗?请。多谢。

{“medicine”:”paracetamol”

“day”:”tuesday”

“time”:”9:00”},

{“medicine”:”aspirine”

“day”:”friday”

“time”:”16:00”},

{“medicine”:”pills”

“day”:”monday”

“time”:”22:00”}

最佳答案

您必须创建一个日历对象,并在 comps.weekday、comps.hour 和 comps.min 属性中设置工作日和时间值。

对于您的消息,请在通知 content.body 属性中设置。

import UserNotifications

let calendar = NSCalendar.init(calendarIdentifier: .gregorian)
calendar?.locale = NSLocale.current

let year = calendar?.component(.year, from: date)
let month = calendar?.component(.month, from: date)

let cal = Calendar.current
var comps = cal.dateComponents([.weekOfYear, .yearForWeekOfYear], from: date)
comps.weekday = i // Monday - Friday
comps.hour = j
comps.minute = minute
comps.year = year
comps.month = month

let notificationDate = cal.date(from: comps)!
let interval = String.init(format: "%d%d%d", i,j,minute)


if #available(iOS 10.0, *) {

let triggerDate = Calendar.current.dateComponents([.weekday,.hour,.minute], from: notificationDate as Date)

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

let content = UNMutableNotificationContent()
content.title = DataManager.sharedInstance.notificationTitle
content.body = String.init(format: "%@", DataManager.sharedInstance.notificationMessage)
content.sound = UNNotificationSound.default()

let request = UNNotificationRequest(identifier: String(interval), content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print(error)
}
}

} else {
// ios 9


// Fallback on earlier versions
let notification = UILocalNotification()

/* Time and timezone settings */
notification.fireDate = date
notification.repeatInterval = NSCalendar.Unit.weekday
notification.timeZone = NSCalendar.current.timeZone
notification.alertBody = "message"

notification.userInfo = [ "title" : String(interval)]

/* Schedule the notification */
UIApplication.shared.scheduleLocalNotification(notification)
}

更新答案

  1. 将您的 json 转换为 NSDictionary 对象的 NSArray。使用 NSJSonSerialization 类。
  2. 对 NSArray 实例运行 for 循环并逐个访问 NSDictionary 对象。
  3. 使用 NSDictionary valueForKey 方法。传递键名并从字典中获取值。

关于ios - 如何按小时和日期显示本地议程或本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45873174/

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