Hi Reader’s,
This blog includes the concept of how to store user information in NSUserDefaults in swift language. Below is the code in swift language for the same:-
import UIKit
import Foundation
class RegisterViewController: UIViewController {
@IBOutlet weak var txtFirstName: UITextField!
@IBOutlet weak var txtLastName: UITextField!
@IBOutlet weak var txtEmailId: UITextField!
@IBOutlet weak var txtPassword: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func btnSignUp(sender: AnyObject) {
// Save user details in NSUserDefaults
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(txtFirstName.text, forKey: "firstName")
defaults.setObject(txtLastName.text, forKey: "lastName")
defaults.setObject(txtEmailId.text, forKey: "emailId")
defaults.setObject(txtPassword.text, forKey: "password")
// Shows alert after saving the user details in NSUserDefaults
let alert = UIAlertController(title: "Alert", message: "User Detail Stored Successfully", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
// Clear the textfields
txtFirstName.text=""
txtLastName.text=""
txtPassword.text=""
txtEmailId.text=""
}
}
0 Comment(s)