简体   繁体   English

应用终止时从NSUserDefaults清除数据

[英]Clear data from NSUserDefaults when app kill

Once I kill application then I want to Clear data from NSUserDefaults, but sometimes it is not getting cleared. 一旦我杀死了应用程序,那么我想从NSUserDefaults清除数据,但是有时它不会被清除。 I have added the code into this method : 我已经将代码添加到此方法中:

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:kcoupondic];

    [[NSUserDefaults standardUserDefaults] synchronize];
}

Why are you using NSUserDefaults if you want to clear values on app termination. 如果要在应用终止时清除值,为什么要使用NSUserDefaults

Use global variable instead of NSUserDefaults 使用全局变量代替NSUserDefaults

OR 要么

If you wan't to remove all NSUserDefaults try this: 如果您NSUserDefaults删除所有NSUserDefaults尝试以下操作:

[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];

applicationWillTerminate is not always called. applicationWillTerminate并不总是被调用。 This is from Apple's documentation. 这来自Apple的文档。

This method lets your app know that it is about to be terminated and purged from memory entirely. 此方法使您的应用知道即将终止并完全从内存中清除。 You should use this method to perform any final clean-up tasks for your app, such as freeing shared resources, saving user data, and invalidating timers. 您应该使用此方法为您的应用执行所有最终清理任务,例如释放共享资源,保存用户数据和使计时器无效。 Your implementation of this method has approximately five seconds to perform any tasks and return. 此方法的实现大约需要五秒钟来执行任何任务并返回。 If the method does not return before time expires, the system may kill the process altogether. 如果该方法在时间到期之前没有返回,则系统可能会完全终止该过程。 For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app. 对于不支持后台执行或与iOS 3.x或更早版本链接的应用程序,总是在用户退出应用程序时调用此方法。 For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. 对于支持后台执行的应用程序,用户退出应用程序时通常不会调用此方法,因为在这种情况下,应用程序只是移至后台。 However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason. 但是,在应用程序在后台运行(未挂起)并且系统出于某种原因需要终止它的情况下,可以调用此方法。

To answer your question as to why your data is not getting cleared, maybe this method is not getting called. 要回答有关为什么未清除数据的问题,也许未调用此方法。 Try and add some logs to check if the method gets called. 尝试添加一些日志以检查是否调用了该方法。

I would rather put this code in applicationDidFinishLaunchingWithOptions inside AppDelegate. 我宁愿将此代码放在AppDelegate内的applicationDidFinishLaunchingWithOptions中 In this case, you're sure that at every app launch you'll clear this data before you re-populate it. 在这种情况下,您可以确保在每次启动应用程序时都将清除此数据,然后再重新填充它。

For Swift: 对于Swift:

if let bundleId = Bundle.main.bundleIdentifier {
    UserDefaults.standard.removePersistentDomain(forName: bundleId)
}

Here, we are optional unwrap bundleIdentifier to prevent crash. 在这里,我们是可选的bundleIdentifier以防止崩溃。

尝试编写代码以清除applicationWillResignActive而不是applicationWillTerminate NSUserDefaults。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase:当用户杀死应用程序时,如何从Firebase中清除特定数据? - Firebase: How to clear specific data from firebase when user kill app? 使用NSUserDefaults杀死应用程序后保存的数据丢失 - Saved data lost after kill app using NSUserDefaults 如何清除NSUserDefaults数据? - How to clear NSUserDefaults Data? 从设备删除应用程序不会清除NSUserDefaults - Deleting app from a device doesn't clear NSUserDefaults 获得“ <NULL> 保存在NSUserDefaults中时,核心数据和应用程序中的数据崩溃了吗? - Getting “<NULL>” data from core data and app crashed when we save in NSUserDefaults? 可以在为我杀死应用程序时清除推送通知吗? - Can clear push a notification when kill app for me? 当应用重新运行时,如何使用NSUserDefaults从ViewController保存数据并将其检索到TableViewController - How to use NSUserDefaults to save data from ViewController and retrieve it to TableViewController when app reruns 清除特定NSUserDefaults对象数据的数据 - Clear data of a particular NSUserDefaults object Data 如何在更新的应用程序上清除NSUserDefaults - How to clear out NSUserDefaults on an updated app 应用更新时是否删除了使用 NSUserDefaults 存储的数据? - Is the data stored using NSUserDefaults deleted when the app is updated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM