从iPhone删除应用程序时如何删除所有本地通知 [英] How to delete all local notifications when an application is deleted from an iPhone

查看:143
本文介绍了从iPhone删除应用程序时如何删除所有本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let's say I set 5 local notification for an iPhone application, then the user deletes the app. If the app is installed again, it shows the previous notifications.

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

I know the following code deletes all notifications

[[UIApplication sharedApplication] cancelAllLocalNotifications];

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

But where do I put that code so that it executes when the application is deleted?

或任何其他解决此问题的方法。

Or any other way to solve this problem.

推荐答案

正如其他人所说,我认为实现此目的的最佳方法是在应用程序委托中使用 didFinishLaunchingWithOptions 中的标记来检查它是否是第一次启动。

As others have mentioned, I think the best way to accomplish this is to have a flag in didFinishLaunchingWithOptions in the application delegate that checks if it is the first launch or not.

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

If the app is being launched for the first time, you can call

[[UIApplication sharedApplication] cancelAllLocalNotifications];

这将取消任何现有通知。

This will cancel any existing notifications.

例如:

- (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;
    }
{

这篇关于从iPhone删除应用程序时如何删除所有本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆