I would like to save data on my app, I used DataCore to save, but it save data in an entity, and an attribute in this entity can save many different values. I need the data is saved only one value for each attribute as preference in Android. How could I do that? Please help me. Thank you. This is my code:
@IBAction func SaveAll(sender: AnyObject) {
let username:String = user_name.text!
let password:String = pass_word.text!
let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext
let newStu = NSEntityDescription.insertNewObjectForEntityForName("Student", inManagedObjectContext: context) as NSManagedObject
newStu.setValue(username, forKey: "userName")
newStu.setValue(password, forKey: "passWord")
do {
try context.save()
} catch {}
}
The store data into NSUserDefaults
is a very traditional way to store the data into memory and it's also a very smooth way to get and set the data. But when you think about the security of your data then NSUserDefaults
will not help here. I mean NSUserDefaults
is not secure and encrypted, so it will easily breakable. For general data storage NSUserDefaults
will help surly but when you want to store sensitive data like username and password then you must have to think about data security.
I always prefer to use Keychain Service API to store such data. Keychain offers a secure alternative to saving sensitive data. Apple has provided the Keychain Services API
to deal with this problem and help developers build apps that safely handle passwords and other sensitive information.
You can find Apple's document here
There are many wrappers exist as Cocoapods or extension libraries on Github and other dependency management sites. Below is some reference which I found.
1.) SwiftKeychainWrapper
2.) Locksmith
Apple’s own Keychain wrapper is called GenericKeychain
and is available within the sample code in both Objective-C
and Swift
.
I think this answer may not accurately relevant to this question but this might help some people in the community.
Try SwiftKeyChainWrapper - Link
It's a useful library for save sensitive data on the device.
Usage
Add a string value to keychain:
let saveSuccessful: Bool = KeychainWrapper.standard.set("Some String", forKey: "myKey")
Retrieve a string value from keychain:
let retrievedString: String? = KeychainWrapper.standard.string(forKey: "myKey")
Remove a string value from keychain:
let removeSuccessful: Bool = KeychainWrapper.standard.removeObject(forKey: "myKey")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With