Reputation: 7340
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
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
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
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
Reputation: 982
You need to use Keychain, to sore sensible information regarding app.
Here is an example how to achieve that.
UserDefaults will get delete when user uninstall app from device, so the alternative is Keychain.
Upvotes: 6