获取每周触发的UILocalNotification [英] Get a UILocalNotification to fire every week

查看:84
本文介绍了获取每周触发的UILocalNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已设置要触发的本地通知。我想知道如何使其在每周的同一时间重复,例如星期一上午9点​​。

I have set up a local notification to fire. I am wondering how to get it to repeat every week at the same time e.g., 9 AM on Monday.

到目前为止,这是我的代码:

Here's my code so far:

 @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) 


推荐答案

您可以在以NSCalendarUnit作为其类型的本地通知上设置一个repeatInterval。您可以在此处 https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/c/tdef/NSCalendarUnit

There is a repeatInterval you can set on the local notification that takes an NSCalendarUnit as its type. You can read more about the different available calendar units here https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSCalendar_Class/#//apple_ref/c/tdef/NSCalendarUnit

您可能正在寻找NSCalendarUnitWeekOfYear

You would likely be looking for NSCalendarUnitWeekOfYear

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

So you could just add the following to your code for your notification

notification.repeatInterval = NSCalendarUnit.WeekOfYear

这篇关于获取每周触发的UILocalNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆