gpt4 book ai didi

ios - cancelLocalNotification 不起作用

转载 作者:行者123 更新时间:2023-11-28 21:42:50 25 4
gpt4 key购买 nike

我的应用程序中有大约 10 个本地通知(不在数组中)及其开关。我用了这段代码

    NSUserDefaults *defaultsB = [NSUserDefaults standardUserDefaults];
UILocalNotification *notification = [[UILocalNotification alloc]init];

if (switcher.on == 1) {

NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:[NSDate date]];

[dateComponent setWeekday:1]; // For sunday
[dateComponent setHour:[timeHClass.text integerValue]];
[dateComponent setMinute:[timeMClass.text integerValue]];

NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];


[notification setAlertBody:textClass1.text];
[notification setFireDate:fireDate];
notification.soundName = @"bells.mp3";
notification.repeatInterval = NSWeekCalendarUnit;
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];


[defaultsB setObject:@"ON" forKey:@"SwitchState"];
}
else {

UIApplication *app=[UIApplication sharedApplication];
[app cancelLocalNotification:notification];



[defaultsB setObject:@"OFF" forKey:@"SwitchState"];

}

但是当我关闭通知时,通知仍然有效??

我应该将通知保存在 NSUserDefaults 中而不仅仅是开关吗??

如果是,怎么办??

最佳答案

如果您想取消现有通知,您需要通过 [[UIApplication sharedApplication] scheduledLocalNotifications] 获取对它的引用,然后遍历这些通知并找到您想要的通知(可能使用唯一的您在创建期间为其提供的标识符)。

或者,如果您想取消所有通知替换:

[app cancelLocalNotification:notification];

与:

[app cancelAllLocalNotifications];

关于ios - cancelLocalNotification 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31373778/

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