iOS应用程序已从后台关闭

时间:2014-11-15 05:33:48

标签: ios objective-c ios8 nsuserdefaults

我创建了一个iOS应用程序,使用NSUserDefaults。如果应用程序从后台关闭,我想删除存储在NSUserDefaults中的字符串。任何人都可以给我找到解决方案该应用程序已从后台关闭。

2 个答案:

答案 0 :(得分:1)

当您的应用从后台运行中删除时,您不会收到任何通知。该应用程序被操作系统杀死,没有任何警告。

当您的应用进入后台时,您无法知道是否会重新开始或只是返回到前台。

你为什么还需要这个?如果你在NSUSerDefaults中保存了一些内容,那么它应该是持久的。如果可能,请告诉我们您需要此功能的方案,以便我们为您提供其他方法。

答案 1 :(得分:1)

您可以通过两种方式清除NSUserDefaults,一个 applicationWillTerminate 另一个 didFinishLaunchingWithOptions 仅在初始时间调用

// if u terminated your application surly it remove the all keys and other local DB 

- (void)applicationWillTerminate:(UIApplication *)application
{


 [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"addyourkeys"]; 
 [[NSUserDefaults standardUserDefaults] synchronize];

 }

选择No:2

   // it is called only initially time , u can also follow your work at here.. but no need at here the applicationWillTerminate will follow the work 

- (void)applicationDidFinishLaunching:(UIApplication *)application
{


 [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"addyourkeys"]; 
 [[NSUserDefaults standardUserDefaults] synchronize];

 }