【发布时间】:2015-08-05 00:02:20
【问题描述】:
我有这段代码,它每天早上 7 点运行通知,它获取当前日期,然后在到达设定时间时运行通知,我的问题是如果时间已经超过设定的运行时间,那么每天都会在用户当前时间而不是我早上 7 点的时间运行,这是我的代码
var dateFire: NSDateComponents = NSDateComponents()
var getCurrentYear = dateFire.year
var getCurrentMonth = dateFire.month
var getCurrentDay = dateFire.day
dateFire.year = getCurrentYear
dateFire.month = getCurrentMonth
dateFire.day = getCurrentDay
dateFire.hour = 7
dateFire.minute = 0
dateFire.timeZone = NSTimeZone.defaultTimeZone()
var calender: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var date: NSDate = calender.dateFromComponents(dateFire)!
var localNotification = UILocalNotification()
localNotification.fireDate = date
localNotification.alertBody = "A new day has begun and a fresh layer on snow lies on the mountain! Can you beat your highscore?"
localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
如您所见,NSCalendarUnit.CaldendarUnitDay 每天早上 7 点运行。我不知道,即使时间是早上 7 点之后,通知仍然会在第二天运行,将不胜感激
【问题讨论】:
-
您需要检查当前时间 - 如果是在 7:00 之后,请将开火日期设置为明天,而不是今天。
标签: ios swift nsdate localnotification