user2727195
user2727195

Reputation: 7340

saving data specific to an app and preserving even after app deletion

Is there a way to preserver some data even after app uninstalls and is retrievable after app gets installed again?

I found NSUserDefault but I'm not sure. please advise.

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("Coding Explorer", forKey: "userNameKey")

Upvotes: 2

Views: 3414

Answers (5)

Maysam R
Maysam R

Reputation: 969

use this method to save Data:

   func saveIMEI(key: String, data: String) -> OSStatus {
    let query = [
        kSecClass as String       : kSecClassGenericPassword as String,
        kSecAttrAccount as String : key,
        kSecValueData as String   : data ] as [String : Any]

    SecItemDelete(query as CFDictionary)

    return SecItemAdd(query as CFDictionary, nil)
}

use this to load Data:

     func loadIMEI(key: String) -> String? {
    let query = [
        kSecClass as String       : kSecClassGenericPassword,
        kSecAttrAccount as String : key,
        kSecReturnData as String  : kCFBooleanTrue,
        kSecMatchLimit as String  : kSecMatchLimitOne ] as [String : Any]

    var dataTypeRef: AnyObject? = nil

    let status: OSStatus = SecItemCopyMatching(query as CFDictionary, &dataTypeRef)

    if status == noErr {
        return dataTypeRef as! String?
    } else {
        return nil
    }
}

Upvotes: -1

Maury Markowitz
Maury Markowitz

Reputation: 9303

Personally I wouldn't use keychain for this, it's simply way too heavyweight for something simple like this.

You might find some luck using defaults domains which allow you to place small bits of data "outside" the normal defaults area. But that too seems like more than what you need.

Another option would be to make your own plist and then save it to iCloud.

Upvotes: 1

Abdul Yasin
Abdul Yasin

Reputation: 3508

This is done using the keychain on iOS most likely.

But from my point of view, Leaving data after uninstallation goes against the sandboxing of applications. There are ways to do it, like you can add images/photos/contacts/..., but unlikely to be "Apple approved" and more than likely easy to hack around.

So I also suggest you to use a web service. A simple way would be to validate with the service (e.g. using the device's MAC address since the device unique identifier is going away) when no application data is found (install and re-install) if it's a know device (no token) or not (get tokens).

Upvotes: 0

Dhruvik
Dhruvik

Reputation: 982

You need to use Keychain, to sore sensible information regarding app.

Here is an example how to achieve that.

KeyChain Example

UserDefaults will get delete when user uninstall app from device, so the alternative is Keychain.

Upvotes: 6

Jelly
Jelly

Reputation: 4532

NSUserDefaults gets cleared when the app is uninstalled. The best way to do this I think it's using keychain this also assures you that your data is encrypted and cannot be accessed by others.

Upvotes: 0

Related Questions