Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way could save username and password in device using swift in iOS that's same as preference in Android?

Tags:

ios

swift

cookies

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

}
like image 986
Anh Trinh Avatar asked Nov 08 '15 04:11

Anh Trinh


2 Answers

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.

like image 83
Bhavin_m Avatar answered Oct 19 '22 05:10

Bhavin_m


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")
like image 31
Deeptha Senanayake Avatar answered Oct 19 '22 07:10

Deeptha Senanayake