【发布时间】:2016-06-02 08:33:11
【问题描述】:
我一直在尝试在工作日安排本地通知,但它们似乎是立即触发,而不是在我安排它们时触发。这是我在didFinishLaunchingWithOptions 中的 AppDelegate 中的代码:
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert, categories: nil))
var notification = UILocalNotification()
var components = NSDateComponents()
var calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
components.weekday = 5
components.hour = 9
components.minute = 24
notification.alertBody = "Notification test"
notification.fireDate = calendar?.dateFromComponents(components)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
关于为什么它可能不起作用的任何线索?
【问题讨论】:
-
打印你的开火日期,你会看到的。
-
嗯,打印组件会给我一个正确的结果(我定义的那个),但是 fireDate 给了我 0001-01-01 10:00:45 +0000。 dateFromComponents 是否不像我认为的那样工作?
-
是的。它只是将给定的组件设置为空日期。你也想设置
day而不是weekday。 -
哦,我明白了。我设置了工作日,因为我希望它在一周中的某一天关闭,在这种情况下是星期四。这不是正确的做法吗?
标签: ios swift uilocalnotification