gpt4 book ai didi

Swift 每 x 天创建本地通知

转载 作者:行者123 更新时间:2023-11-30 10:06:44 25 4
gpt4 key购买 nike

我在 Objective C 中找到了很多答案,但我正在寻找一种方法来为每个星期一和星期二,或者每个星期六和星期四等创建本地通知。在我的代码中,我创建了一些 NSDateComponents

let date2 = NSDateComponents()
date2.hour = 4
date2.minute = 3


let date3 = NSDateComponents()
date3.hour = 12
date3.minute = 33

...

然后将它们放入数组

datesArray = [date2, date3]

我发现关于如何在 swift 2 中创建本地通知的资源非常少,到目前为止我所能做的就是在构建和运行应用程序后 10 秒内创建一个本地通知,但这与我创建的日期。

    var localNotification = UILocalNotification()
localNotification.fireDate = NSDate(timeIntervalSinceNow: 10)
localNotification.repeatInterval = NSCalendarUnit.Day
localNotification.alertBody = "Take Out Track"
localNotification.timeZone = NSTimeZone.defaultTimeZone()
localNotification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1

UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

我想做的是为数组中的每个日期创建一个通知,然后能够说只在星期一星期三和星期六或用户选择的任何时间重复该通知。

最佳答案

为此,您必须使用 nscalender,通过它您可以获得工作日,就像 iOS 中的工作日一样,星期日是 1 ,星期一是 2 等等......通过它您可以设置在哪一天必须触发通知。并查看 tuturiol

http://www.appcoda.com/ios-event-kit-programming-tutorial/

关于Swift 每 x 天创建本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35561837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com