swift2 - Storing user ID and password globally? -


i'm trying create login process sending post request web server. post string contains username , password, user types in textfield.

as response user id , sha1 encrypted password. since need both of them access other pages on server, have store them globally in project.

so question is, best way so? should create class user, store items properties?

you can using 2 different ways,

01:-

      let id = username.text       let password = password.text       let displayname = display.text        // stroe data       var prefs:nsuserdefaults = nsuserdefaults.standarduserdefaults()       prefs.setinteger(id!, forkey: "userid") // integer values       prefs.setinteger(password!, forkey: "userpassword")       prefs.setobject(displayname!, forkey: "displayname") // string values       prefs.synchronize()        // stored data         let prefs:nsuserdefaults = nsuserdefaults.standarduserdefaults()       id.text = prefs.valueforkey("userid") as? int       jid.text = prefs.valueforkey("userjid") as? int       displayname.text = prefs.valueforkey("displayname") as? string 

when log out or if want clear stored data, use

nsuserdefaults.standarduserdefaults().removepersistentdomainforname(nsbundle.mainbundle().bundleidentifier!) 

02:-

  1. first create global class,

    import foundation   class user { class var sharedinstance: user { struct static {     static var instance: user?     static var token: dispatch_once_t = 0 }  dispatch_once(&static.token) {     static.instance = user() }  return static.instance! 

    }

    var userid: int? var vid: int? var ishome: int? 

    }

then store data in class want

class viewcontroller: uiviewcontroller {     let user = user.sharedinstance    override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {      if segue.identifier == "showdetails" {               self.user.userid = id             self.user.vid = vacancyid           }      }     } 

then retrieve data :-

 class viewcontroller: uiviewcontroller {     let user = user.sharedinstance               mylabel.text = self.user.userid              mylabael2.text = self.user.vid      } 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -