gpt4 book ai didi

objective-c - UILocalNotification 每周六 12 点 :00 am

转载 作者:搜寻专家 更新时间:2023-10-30 20:25:26 24 4
gpt4 key购买 nike

我尝试在每个星期六重复 UILocalNotification,但没有成功。我尝试以下操作:

// Create notification for every saturday at 12:00 am
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSUIntegerMax fromDate:[NSDate date]];
[components setWeekday:7]; // Saturday
[components setHour:12]; // 12 am
[components setMinute:0];
[components setSecond:0];

NSDate *fireDate = [gregorian dateFromComponents:components];
NSLog(@"%@", fireDate);

[[NotificationsManager sharedManager] configureLocalAlertMessage:message
fireDate:fireDate
repeatIntervalUnit:NSWeekCalendarUnit
type:kNotificationTypeMoneyBox];

但是“fireDate”返回:“2013-03-12 11:00:00 +0000”即今天。

有什么帮助吗?谢谢!

编辑:

配置通知的代码

- (void)configureLocalAlertMessage:(NSString *)message fireDate:(NSDate *)date repeatIntervalUnit:(NSCalendarUnit)repeat type:(NotificationType)type {

// Local notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.timeZone = [NSTimeZone systemTimeZone];
notification.fireDate = date;
notification.repeatInterval = repeat;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.hasAction = YES;
notification.alertBody = message;
notification.userInfo = @{kNotificationTypeKey : @(type)};
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

// Schedule
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

最佳答案

    NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =[gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit | NSWeekCalendarUnit | NSYearCalendarUnit) fromDate:idate];

int weekday = [weekdayComponents weekday];
NSDate *nextdate ;
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
if(weekday <= 5)
{
/* it is my case to check the weekday, if it is less or greater than 5*/
}
else
{
[offsetComponents setDay:7];
[offsetComponents setHour:12];
[offsetComponents setMinute:0];
[offsetComponents setSecond:0];
nextdate = [gregorian dateByAddingComponents:offsetComponents toDate:idate options:0];
}
[offsetComponents release];
[gregorian release];

您可以使用这个下一个日期 作为本地通知的触发日期。这里的idate是NSDate *idate = [NSDate date];

关于objective-c - UILocalNotification 每周六 12 点 :00 am,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15365543/

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