iOS 如何取消预定的本地通知
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,经过一步一步的思考,我找到了解决问题的可行方法。我发现我处理整个问题的方式完全错误!
本质上,我发现当应用程序处于活动状态(即在前台)时,您不需要任何本地通知。因此,我没有在计时器启动时设置本地通知,而是在应用程序即将退出时设置相关通知。
我把这些观察者放在我的 viewDidLoad 中;
在我的 appWillResign 方法中,我为任何活动计时器设置了通知。当应用程序恢复时,我只需取消所有通知即可。
简而言之,您不需要在代码中的其他地方引用通知。您仅在绝对需要时才设置它们:当应用程序处于后台时!因此,您确实不需要将它们“存储”在任何地方以供以后使用。
感谢您的贡献@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;
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.
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
很难预测,但通常当您访问 nil 或已释放的对象时,就会出现 SIGABRT。
因此,在
[[UIApplication sharedApplication] cancelLocalNotification:myNotification];
` 中,它可以是“myNotification”,即 nil。
或者如果
[myDefaults objectForKey:@"myNotification"]返回nil
。可能的原因是
NsUserDefaults
仅存储少数数据类型,如下面的类引用段落中所述。我可以预测的是,正在存储的
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.What i can predict is that
myNotification
which is being stored is of typeUILocalNotification.
so probably storing of data is failing.Please debug the code further and post if above cases are not working.
修复方法是同步
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 toNSUserDefaults
, rather just to the localNSUserDefaults
object. What you need to do is call[defaults synchronize]
after setting the object.