从iPhone删除应用程序时如何删除所有本地通知

Nim*_*ekh 29 iphone xcode objective-c uilocalnotification

假设我为iPhone应用程序设置了5个本地通知,然后用户删除了该应用程序.如果再次安装该应用,则会显示之前的通知.

我知道以下代码会删除所有通知

[[UIApplication sharedApplication] cancelAllLocalNotifications];
Run Code Online (Sandbox Code Playgroud)

但是我在哪里放置代码以便在删除应用程序时执行它?

或任何其他方式来解决这个问题.

Der*_*itt 31

正如其他人所提到的,我认为实现这一目标的最佳方法是在应用程序委托中的didFinishLaunchingWithOptions中设置一个标志,以检查它是否是第一次启动.

如果该应用程序是第一次启动,您可以致电

[[UIApplication sharedApplication] cancelAllLocalNotifications];
Run Code Online (Sandbox Code Playgroud)

这将取消任何现有通知.

例如:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    // this flag will need to be stored somewhere non-volatile such as using CoreData 
    // or user defaults
    if(flag == nil || [flag count] ==0){ 

       [[UIApplication sharedApplication] cancelAllLocalNotifications];

       // update your flag so that it fails this check on any subsequent launches
       flag = 1;
    }
{
Run Code Online (Sandbox Code Playgroud)