Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to store user information in NSUserDefaults in swift language?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 140
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: