获取一个UILocalNotification每周开火
我已经设置了一个本地通知来触发。 我想知道如何让它在每周的同一时间,例如周一上午9点重复。
这是我的代码到目前为止:
@IBAction func scheduleLocal(sender: UIButton) { guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() else { return } if settings.types == .None { let ac = UIAlertController(title: "Cant Schedule", message: "No Permission", preferredStyle: .Alert) ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) presentViewController(ac, animated: true, completion: nil) return } let notification = UILocalNotification() notification.fireDate = NSDate() notification.alertBody = "Come Exercise" notification.alertAction = "Exercise Time" notification.soundName = UILocalNotificationDefaultSoundName notification.userInfo = ["customField1": "w00t"] UIApplication.sharedApplication().scheduleLocalNotification(notification) }
并在viewDidLoad
:
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
有一个repeatInterval可以在本地通知上设置NSCalendarUnit作为它的types。 你可以在这里阅读更多有关不同的日历单元https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/c/tdef/NSCalendarUnit
你可能会在寻找NSCalendarUnitWeekOfYear
所以你可以在你的代码中添加以下代码以获得通知
notification.repeatInterval = NSCalendarUnit.WeekOfYear
你的代码是正确的。
但是:“设备上的每个应用程序仅限于64个预定的本地通知。” https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/WhatAreRemoteNotif.html
您可以安排本地通知,并在用户每次打开应用程序时重新创build它们。 或者使用远程通知。