gpt4 book ai didi

iphone - 是否有可能为 UILocalNotification 设置条件 `firedate`?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:27 25 4
gpt4 key购买 nike

我正在尝试为我的本地通知设置一个有条件的触发时间,但是当我尝试这两种方法时它并没有失败,但它仍然没有用。所以,我想知道我是否能够做这样的事情?

注意:startTime 和 endTime 是来自日期选择器的时间。

-(void) TEST {

NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];

NSDateComponents *components = [calendar components:NSHourCalendarUnit fromDate:[NSDate date]];
components.hour = 3;
components.minute = (components.minute + 1) % 60;
components.second = 57;

NSDate *fire = [calendar dateFromComponents:components];
UILocalNotification *notif = [[UILocalNotification alloc] init] ;
if (notif == nil)
return;

if (fire < startTime.date) {


notif.fireDate =fire ;
notif.repeatInterval= NSMinuteCalendarUnit ;
notif.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
[[UIApplication sharedApplication] scheduleLocalNotification:notif] ;


}

if (fire > endTime.date) {


notif.fireDate =fire ;
notif.repeatInterval= NSMinuteCalendarUnit ;
notif.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
[[UIApplication sharedApplication] scheduleLocalNotification:notif] ;


}}

-(void) TEST {

NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];

NSDateComponents *components = [calendar components:NSHourCalendarUnit fromDate:[NSDate date]];
components.hour = 3;
components.minute = (components.minute + 1) % 60;
components.second = 57;

NSDate *fire = [calendar dateFromComponents:components];
UILocalNotification *notif = [[UILocalNotification alloc] init] ;
if (notif == nil)
return;

if (fire > startTime.date & fire < endTime.date) {

[[UIApplication sharedApplication] cancelAllLocalNotifications] ;
}

else {
notif.fireDate =fire ;
notif.repeatInterval= NSMinuteCalendarUnit ;
notif.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
[[UIApplication sharedApplication] scheduleLocalNotification:notif] ;


}}

谢谢

如果不是,那么最简单的方法是什么?

最佳答案

使用 NSDate Compare: 方法代替 if(fire > startTime.date)

if([fire compare: startTime.date] == NSOrderedDescending) // if start is later in time than end
{
// do something
}

来自类引用:

If:
The receiver and anotherDate are exactly equal to each other, NSOrderedSame.
The receiver is later in time than anotherDate, NSOrderedDescending.
The receiver is earlier in time than anotherDate, NSOrderedAscending.

所以对于你的情况

if (fire > startTime.date & fire < endTime.date) {

[[UIApplication sharedApplication] cancelAllLocalNotifications] ;
}

你可以使用

if([fire compare: startTime.date] == NSOrderedDescending && [fire compare: endTime.date]== NSOrderedAescending)
{
[[UIApplication sharedApplication] cancelAllLocalNotifications] ;
}

如果火灾发生在选定的开始和结束日期之间,这将取消所有本地通知

else {
// Fire the notifications
}

另请查看链接,这与您需要的相似 link

关于iphone - 是否有可能为 UILocalNotification 设置条件 `firedate`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16380689/

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