10

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 {}

}
Anh Trinh
  • 157
  • 1
  • 3
  • 12
  • 4
    If you are going to be storing sensitive information like passwords, I strongly recommend looking into the iOS keychain APIs – 72A12F4E Nov 08 '15 at 05:03
  • Thanks for your answer. I mean after user enter a username or a password, this username will be saved as a value only in the memory of user's device. And when user open the app again, that saved value of username could be used again to do something. Could the keychain do that? – Anh Trinh Nov 08 '15 at 15:41
  • 1
    definitely. This link outlines how keychain works and how to use it. https://developer.apple.com/library/mac/documentation/Security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html Its a kludgy C API, but it does work. Alternatively, here is a much more usable wrapper around those APIs that i use in most of my projects. https://github.com/kishikawakatsumi/UICKeyChainStore – 72A12F4E Nov 08 '15 at 16:41
  • Do not store passwords in plain text. – Raptor Dec 18 '18 at 07:13

3 Answers3

6

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")
3

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.

Bhavin_m
  • 2,746
  • 3
  • 30
  • 49
0

Use Shared Web Credentials

How to use:

https://stackoverflow.com/a/66951693/11662833

Sreekuttan
  • 1,579
  • 13
  • 19