iOS 如何取消预定的本地通知

发布于 2024-12-04 01:11:06 字数 693 浏览 2 评论 0原文

我正在编写一个 iPhone 应用程序,其中有三个倒计时器,每个计时器在用户启动时设置一个本地通知(IBAction)。

我的问题是,如何在代码中的其他地方引用现有的本地通知?我尝试像这样直接引用它,这会崩溃:

[[UIApplication sharedApplication] cancelLocalNotification:myNotification];

在安排通知并将其添加到用户默认值之后,像这样:

In my scheduling method...
[myDefaults setObject:myNotification forKey:@"myNotification"];
...

And in my cancelling method...
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[[UIApplication sharedApplication] cancelLocalNotification:[myDefaults objectForKey:@"myNotification"]];
[myDefaults synchronize];

我的应用程序因上面的 cancelLocalNotification 行上的 SIGABRT 错误而崩溃。谁能告诉我哪里出错了?

提前致谢!

I am writing an iPhone app in which I have three countdown timers, that each set a local notification when they are started by the user (IBAction).

My question is, how can I reference to an existing local notification elsewhere in the code? I have tried referencing it directly like this which crashes:

[[UIApplication sharedApplication] cancelLocalNotification:myNotification];

And like this after scheduling the notification and adding it to the user defaults:

In my scheduling method...
[myDefaults setObject:myNotification forKey:@"myNotification"];
...

And in my cancelling method...
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
[[UIApplication sharedApplication] cancelLocalNotification:[myDefaults objectForKey:@"myNotification"]];
[myDefaults synchronize];

My app crashes with a SIGABRT error on the cancelLocalNotification line above. Can anybody tell me where I am going wrong?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉溺在你眼里的海 2024-12-11 01:11:06

好的,经过一步一步的思考,我找到了解决问题的可行方法。我发现我处理整个问题的方式完全错误!

本质上,我发现当应用程序处于活动状态(即在前台)时,您不需要任何本地通知。因此,我没有在计时器启动时设置本地通知,而是在应用程序即将退出时设置相关通知。

我把这些观察者放在我的 viewDidLoad 中;

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appWillResign) name:UIApplicationWillResignActiveNotification 
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appIsActive) name:UIApplicationDidBecomeActiveNotification 
object:nil];

在我的 appWillResign 方法中,我为任何活动计时器设置了通知。当应用程序恢复时,我只需取消所有通知即可。

[[UIApplication sharedApplication] cancelAllLocalNotifications];

简而言之,您不需要在代码中的其他地方引用通知。您仅在绝对需要时才设置它们:当应用程序处于后台时!因此,您确实不需要将它们“存储”在任何地方以供以后使用。

感谢您的贡献@Learner,您帮助我走上了正轨! :D

Ok, I've found a working solution to my issue after thinking it through step by step. I found I was going about the whole issue entirely the wrong way!

Essentially, I found you don't need any local notifications when the app is active (i.e. in foreground). So instead of setting the local notification when the timer is started, I set the relevant notifications up when the app is about to resign.

I put these observers in my viewDidLoad;

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appWillResign) name:UIApplicationWillResignActiveNotification 
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appIsActive) name:UIApplicationDidBecomeActiveNotification 
object:nil];

and in my appWillResign method, I set up the notifications for any active timers. When the app is resumed, I simply cancel ALL the notifications.

[[UIApplication sharedApplication] cancelAllLocalNotifications];

In short, you shouldn't need to reference the notifications elsewhere in the code. You only set them up when they are absolutely needed: when the app is backgrounded! So you really don't need to 'store' them anywhere for later use.

Thanks for your contribution @Learner, you helped put me on the right track! :D

就此别过 2024-12-11 01:11:06

很难预测,但通常当您访问 nil 或已释放的对象时,就会出现 SIGABRT。

因此,在 [[UIApplication sharedApplication] cancelLocalNotification:myNotification]; ` 中,

它可以是“myNotification”,即 nil。

或者如果[myDefaults objectForKey:@"myNotification"]返回nil

可能的原因是 NsUserDefaults 仅存储少数数据类型,如下面的类引用段落中所述。

默认对象必须是属性列表,即 NSData、NSString、NSNumber、NSDate、NSArray 或 NSDictionary 的实例(或集合的实例组合)。如果您想存储任何其他类型的对象,通常应该将其存档以创建 NSData 的实例。

我可以预测的是,正在存储的 myNotification 类型为 UILocalNotification。 因此数据存储可能会失败。

如果上述情况不起作用,请进一步调试代码并发布。

It is difficult to predict but generally SIGABRT comes when you access an nil or released object.

So in [[UIApplication sharedApplication] cancelLocalNotification:myNotification]; `

it can be "myNotification" which is nil.

OR if [myDefaults objectForKey:@"myNotification"] returns nil.

Possible resason for that can be that NsUserDefaults stores only few data type as mentioned in following paragraph of class reference.

A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. If you want to store any other type of object, you should typically archive it to create an instance of NSData.

What i can predict is thatmyNotification which is being stored is of type UILocalNotification. so probably storing of data is failing.

Please debug the code further and post if above cases are not working.

断桥再见 2024-12-11 01:11:06

修复方法是同步 NSUserDefaults。我假设您只是设置了对象但没有同步它。如果是这样,该对象实际上不会保存到 NSUserDefaults 中,而只是保存到本地 NSUserDefaults 对象中。您需要做的是在设置对象后调用[defaults Synchronize]

The fix is to sync the NSUserDefaults. I'm assuming you just set the object and did not synchronize it. If so the object will not actually save to NSUserDefaults, rather just to the local NSUserDefaults object. What you need to do is call [defaults synchronize] after setting the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文