gpt4 book ai didi

ios - 无法快速安排本地通知在特定时间和日期触发

转载 作者:搜寻专家 更新时间:2023-11-01 05:41:59 24 4
gpt4 key购买 nike

我正在尝试在特定时间快速安排通知。我的 TimeString 是一个看起来像 08:32 或 12:23 的字符串。当我尝试在这个特定的日期和时间触发时,通知只在特定的日期触发,而不是在特定的时间触发。例如,如果我将年份更改为 2016 年,它就不会触发。如果我更改月份,它不会触发。然而,无论我输入什么时间,如果日期与当前日期匹配,我在 2015 年 1 月 28 日写这篇文章,无论时间如何,通知都会触发,事实上,只要我关闭应用程序,它就会触发,因为我的代码是在

func applicationDidEnterBackground(application: UIApplication) {
}

我的代码确实可以编译,所以我认为我的代码中没有错误。

有什么方法可以在特定时间触发通知,而不仅仅是在 swift 中的日期,如果可以,我该如何实现。

            var timeString : String =  "2015-01-28 " + TimeString;
println(timeString);

let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm"
formatter.timeZone = NSTimeZone(abbreviation: "GMT");
var date = formatter.dateFromString(timeString)



var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "Block"
localNotification.alertBody = block.description() + " Started";
localNotification.fireDate = date
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

最佳答案

尝试使用这种方法来设置您的日期:

let calendar = NSCalendar.currentCalendar()
let calendarComponents = NSDateComponents()
calendarComponents.hour = 8 //you can get you time components
calendarComponents.second = 0 //from your string rather than
calendarComponents.minute = 0 //using fixed values
calendarComponents.day = 28
calendarComponents.year = 2015
calendarComponents.month = 1
let dateToFire = calendar.dateFromComponents(calendarComponents)

它对我有用。希望这可以帮助。 :)

关于ios - 无法快速安排本地通知在特定时间和日期触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28204626/

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