简体   繁体   English

删除应用程序后,NSUserDefaults initWithSuiteName会保持不变

[英]NSUserDefaults initWithSuiteName persisting after deleting app

I have an issue where if I store any data using [[NSUserDefaults alloc] initWithSuiteName:SUITE_NAME]] the data persists even after deleting the app. 我有一个问题,如果我使用[[NSUserDefaults alloc] initWithSuiteName:SUITE_NAME]]存储任何数据,即使删除应用程序后数据仍然存在。 Is this supposed to happen? 这应该发生吗?

This is supposed to happen. 这应该发生。 This is in fact the purpose of initWithSuiteName , to share data between all apps in the app group. 这实际上是initWithSuiteName的目的,用于在应用程序组中的所有应用程序之间共享数据。 The docs say: 文档说:

Use this method in scenarios such as: 在以下情况下使用此方法:

  • When developing an app suite, to share preferences or other data among the apps 开发应用程序套件时,在应用程序之间共享首选项或其他数据

  • When developing an app extension, to share preferences or other data between the extension and its containing app 开发应用扩展程序时,在扩展程序及其包含的应用程序之间共享首选项或其他数据

There is no way for an app to delete this, because when an app is deleted it is not notified. 应用无法删除此内容,因为删除某个应用时,系统不会通知该应用。 This is the same issue with storing items in the Keychain -- they persist beyond the life of the app. 将项目存储在钥匙串中也是同样的问题 - 它们会持续超出应用程序的生命周期。 This can be a good or a bad thing depending on your app's needs. 根据您应用的需求,这可能是好事也可能是坏事。

One solution is to encrypt the appropriate parts of contents of the file and store the key in a shared keychain. 一种解决方案是加密文件内容的适当部分并将密钥存储在共享密钥链中。 For something like this, a random 256-bit AES symmetric key is perfect. 对于这样的事情,随机的256位AES对称密钥是完美的。 Generally though, as long as the user has a passcode on the device, the filesystem is encrypted and items in the shared storage should be considered clean. 通常,只要用户在设备上有密码,文件系统就会被加密,共享存储中的项目应该被认为是干净的。

Another option is to use shared storage as a passthrough; 另一种选择是使用共享存储作为传递; we do this in our suite of apps. 我们在应用程序套件中执行此操作。 One app will put an encrypted file in shared storage, then call another app in our suite with the file URI and the encryption key. 一个应用程序将加密文件放在共享存储中,然后使用文件URI和加密密钥调用我们套件中的另一个应用程序。 The receiving app will copy the file into its local storage, remove the shared file and then decrypt the local file. 接收应用程序将文件复制到其本地存储,删除共享文件,然后解密本地文件。

As others have mentioned, defaults stored in a shared suite can persist despite deletion of the app and there is no way of detecting app deletion and cleaning up these defaults. 正如其他人所提到的,尽管删除了应用程序,但存储在共享套件中的默认值仍然存在,并且无法检测应用程序删除并清除这些默认值。 However, there is a way to deal with it in the case of somebody reinstalling the app (and you don't want the old defaults to be there). 但是,在有人重新安装应用程序的情况下,有一种方法可以处理它(并且您不希望旧的默认值存在)。

To deal with this, you can simply save a bool to the standard user defaults on startup of the app. 要解决这个问题,您可以在应用启动时将bool保存到标准用户默认值。 If the bool is not present when you start the app (as it is the first installation), then you can purge the shared suite defaults to make it start up as a fresh install. 如果启动应用程序时bool不存在(因为这是第一次安装),那么您可以清除共享套件默认值,使其作为全新安装启动。 In this way, on a fresh install you'll have an empty shared defaults. 这样,在全新安装中,您将拥有一个空的共享默认值。

For example: 例如:

let defaults = NSUserDefaults.standardUserDefaults()
if !defaults.boolForKey("firstInstallComplete") {
    let sharedSuite = NSUserDefaults(suiteName: "com.example.app.shared")!

    // delete whatever keys you want
    sharedSuite.removeObjectForKey("example")
    sharedSuite.removeObjectForKey("another")
    ...
    sharedSuite.synchronize()

    defaults.setBool(true, forKey: "firstInstallComplete")
    defaults.synchronize()
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM