func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let settings = UIUserNotificationSettings(forTypes: .Badge, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
let localNotification1 = UILocalNotification()
localNotification1.alertBody = "Your alert message 111"
localNotification1.timeZone = NSTimeZone.defaultTimeZone()
localNotification1.fireDate = self.getEightAMDate()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification1)
let localNotification2 = UILocalNotification()
localNotification2.alertBody = "Your alert message22"
localNotification2.timeZone = NSTimeZone.defaultTimeZone()
localNotification2.fireDate = self.getSevenPMDate()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification2)
return true
}
func getEightAMDate() -> NSDate? {
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now: NSDate! = NSDate()
let date10h = calendar.dateBySettingHour(8, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!
return date10h
}
func getSevenPMDate() -> NSDate? {
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now: NSDate! = NSDate()
let date19h = calendar.dateBySettingHour(19, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)!
return date19h
}
嘿!感謝您的回覆....快一個...希望你能告訴我如何將時間設置爲8:00 AM/7 PM,因爲NSDate API沒有給我一個直接設置時間的選項。只有'timeIntevalSinceNow/1970 /參考'等。或者我應該使用其中之一?如果是的話,我該怎麼辦呢?謝謝 –
我已經更新了答案並嘗試過。它的工作原理 – ronan
感謝一羣夥伴。我想日曆是我不知道的。看着周圍,有幾個人有同樣的問題。顯然日曆API不會在文檔中更新。但我必須證實這一點。無論如何,你是英雄隊友! –