问题
I just want to ensure I understand how local notifications work. Is it true that once I run the below code one time, I'll have weekly notifications?
//Goes in AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//Added this for notifications.
//Without this, I don't believe the user gets the opportunity to approve notifications from the app
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
return true
}
//Goes in viewController
func weeklyNotifications () {
let localNotification = UILocalNotification()
localNotification.fireDate = NSDate(timeIntervalSinceNow: 60*60)
localNotification.alertBody = "This is the message"
localNotification.timeZone = NSTimeZone.localTimeZone()
localNotification.repeatInterval = NSCalendarUnit.WeekOfYear
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.category = "Message" //Optional
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
In other words, the result of this code is that the first time I run the app, I can execute this code and the notification will repeat every week without me running additional code?
回答1:
localNotification.repeatInterval = NSCalendarUnit.WeekOfYear
Yes this will schedule local notification for every week.
来源:https://stackoverflow.com/questions/35601515/recurring-local-notification-in-ios