迅速iOS本地通知

我想设置特定时间的本地通知,例如:上午04:30,每天,但我无法弄清楚。 我是新的iOS开发和所有localNotifications教程只包括fireDate = NSDate()

谢谢!

试试这个代码在你想要触发本地通知的函数中:

let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) let date = NSDate() let dateComponents = calendar!.components([NSCalendarUnit.Day, NSCalendarUnit.WeekOfMonth, NSCalendarUnit.Month,NSCalendarUnit.Year,NSCalendarUnit.Hour,NSCalendarUnit.Minute], fromDate:date) dateComponents.hour = 4 dateComponents.minute = 30 let notification = UILocalNotification() notification.alertAction = "Title" notification.alertBody = "Fire Fire Fire :v" notification.repeatInterval = NSCalendarUnit.WeekOfYear notification.fireDate = calendar.dateFromComponents(dateComponents) UIApplication.sharedApplication().scheduleLocalNotification(notification) 

并记住把这个代码放在你的AppDelegate中:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Badge, .Alert], categories: nil)) return true }