即使代码中有 none,我的应用程序仍会继续提供 UILocalNotification

My app keeps giving UILocalNotifications even though there are none in the code

我在我的代码中加入了一些 UILocalNotifications 来告诉我何时跨越了我在我的应用程序中编程的某些地理障碍。但是,我已经删除了这些通知。我什至在我的 mac 上输入了 "UILocalNotification" 并看到 none 我的项目文件包含这个词。那么,为什么我仍然收到这些通知?我已经从我的 phone 中删除了该应用程序,然后重新安装了它,但我仍然可以使用它们。

UILocalNotifications 由 iOS 本身(实际操作系统)安排,即一旦您的应用安排了通知,它就会位于 OS 并等待触发(或是 cancelled)。这就是为什么删除应用程序无济于事的原因。通知不在应用程序中,而是在系统中。

您可以通过在 AppDelegate 中调用一次(例如在 didFinishLaunching 中)来解决此问题:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

一旦你调用这条线,你的应用程序已经安排的所有通知都将被取消。

如果您只是想知道当前安排了多少通知,您可以使用

检查retrieving all the notifications
NSArray *scheduledNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];