gpt4 book ai didi

ios - 在特定日期触发UILocalNotification

转载 作者:行者123 更新时间:2023-12-01 17:57:00 25 4
gpt4 key购买 nike

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

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点时,则不能正常工作。

如何将通知生效日期设置为“下一个下午4点”?

最佳答案

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

关于ios - 在特定日期触发UILocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15636106/

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