在特定日期触发UILocalNotification

编程入门 行业动态 更新时间:2024-10-10 04:26:57
本文介绍了在特定日期触发UILocalNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在特定的日期触发UILocalNotification.如果我使用此代码:

I want to fire a UILocalNotification in a specific Date. If I use this code:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]]; [components setHour:4]; [components setMinute:0]; NSDate *fireDate = [gregorian dateFromComponents:components];

如果现在是下午3点,则可以正常工作,但例如到下午5点时,则不能正常工作.

If now it's 3 pm this works fine, but it doesn't when, for example, it's 5 pm.

如何将通知触发日期设置为下一个下午4点"?

How can I set the notification fire date to "the next 4 pm" ?

推荐答案

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]]; [components setHour:4]; [components setMinute:0]; NSDate *fireDate = [gregorian dateFromComponents:components]; NSLog(@"Fire date : %@",fireDate);// Today 4pm is already passed. So you wont get notification // You need to check if the time is already passed if ([fireDate compare:[NSDate date]] == NSOrderedAscending) { // Schedule it for next day 4pm components.day = components.day+1; fireDate = [gregorian dateFromComponents:components]; } NSLog(@"Fire date : %@",fireDate);

更多推荐

在特定日期触发UILocalNotification

本文发布于:2023-11-24 12:08:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625143.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   在特定   UILocalNotification

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!