Is there a way to save the username and password on the device using swift on iOS as on Android?

I would like to save the data in my application, I used the DataCore to save, but it saves the data in essence, and the attribute in this object can store many different values. I need the data to be saved only one value for each attribute as a preference in Android. How could I do this? Please help me. Thanks. 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 {} } 
+8
ios cookies swift
source share
3 answers

You must use UserDefaults for this. This allows you to store insecure data in your preferences. In addition, instead of a password, you can store some kind of session management key that you get from the server every time you log in after a certain period. This would be a little safer than just storing a password.

Thanks to UserDefaults you save everything you want in a key-value repository that is constantly stored on your phone, and you don’t have to bother with complex CoreData stuff.

Just do it like that

 @IBAction func SaveAll(sender: AnyObject) { let username:String = user_name.text! let password:String = pass_word.text! let defaults = UserDefaults.standard defaults.set(username, forKey: "username") defaults.set(password, forKey: "password") } 

You can then call the corresponding defaults.string(forKey: "username") functions defaults.string(forKey: "username") and password functions to easily get them.

+8
source share

NSUserDefaults data in NSUserDefaults is a very traditional way to store data in memory, as well as a very smooth way to get and set data. But when you think about the security of your data, then NSUserDefaults will not help here. I mean, NSUserDefaults not NSUserDefaults and are not encrypted, so it's easy to crack. NSUserDefaults will help with the general storage of data NSUserDefaults , but when you want to store confidential data such as username and password, then you should think about data security.

I always prefer to use the Keychain Service API to store such data. The keychain offers a safe alternative to maintaining confidential data. Apple has provided the Keychain Services API to solve this problem and helps developers create applications that securely handle passwords and other sensitive information.

You can find the Apple document here

There are many shells in the form of Cocoapods or extension libraries on Github and other dependency management sites. Below is the link I found.

1.) SwiftKeychainWrapper

2.) Locksmith

Apple's own Keychain wrapper is called GenericKeychain and is available in sample code in Objective-C and Swift .

I think this answer may not be relevant to this question, but it may help some people in the community.

+2
source share

Try SwiftKeyChainWrapper - Link

This is a useful library for storing sensitive data on the device.

using

Add a string value to the keychain:

 let saveSuccessful: Bool = KeychainWrapper.standard.set("Some String", forKey: "myKey") 

Get string value from keychain:

 let retrievedString: String? = KeychainWrapper.standard.string(forKey: "myKey") 

Remove string value from keychain:

 let removeSuccessful: Bool = KeychainWrapper.standard.removeObject(forKey: "myKey") 
0
source share

All Articles