假设我为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删除应用程序时如何删除所有本地通知
发布评论