我有一个每日通知,提醒用户使用该应用程序。我希望文本不是静态的。我说了 5 个不同的字符串,如果可能的话,我希望随机选择字符串。
我当前的代码,每天都使用相同的文本:
let date: NSDate = NSDate()
let cal: NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
let newDate: NSDate = cal.dateBySettingHour(22, minute: 00, second: 0, ofDate: date, options: NSCalendarOptions())!
var localNotification: UILocalNotification = UILocalNotification()
localNotification.alertAction = "MyApp"
localNotification.alertBody = "Call to action text"
localNotification.fireDate = newDate
localNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
最佳答案
您可以根据星期几使用 switch
语句。这是 excellent NSHipster tutorial on NSDate components 。这是一些sample code回复:如何从 NSDate 中提取一周中的某一天:
func getDayOfWeek(today:String)->Int {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let todayDate = formatter.dateFromString(today)!
let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let myComponents = myCalendar.components(.CalendarUnitWeekday, fromDate: todayDate)
let weekDay = myComponents.weekday
return weekDay
}
let weekday = getDayOfWeek("2014-08-27")
println(weekday) // 4 = Wednesday
Here's Apple's documentation对于 switch
语句。
基本上,您需要创建一个方法,将 NSDate
作为输入并输出一周中的某一天。获得星期几后,您可以在 switch
语句中创建自定义消息。
关于ios - 如何更改每日通知中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32576550/