获取UILocalNotification每周开火

时间:2016-03-03 19:36:55

标签: ios swift cocoa-touch 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) 

2 个答案:

答案 0 :(得分:2)

您可以在本地通知上设置repeatInterval,该通知将NSCalendarUnit作为其类型。您可以在此处详细了解不同的可用日历单元https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/c/tdef/NSCalendarUnit

您可能正在寻找NSCalendarUnitWeekOfYear

因此,您只需将以下内容添加到您的通知代码中

即可
notification.repeatInterval = NSCalendarUnit.WeekOfYear

答案 1 :(得分:0)

您的代码是对的。

但: “设备上的每个应用仅限于64个预定的本地通知。” https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/WhatAreRemoteNotif.html

您可以安排本地通知,并在每次用户打开应用时重新创建它们。或者使用远程通知。