2

If I have a logged in user and stored his id inside the app using

NSUserDefaults.standardUserDefaults().setBool(true, forKey:"isUserLoggedIn");
                            NSUserDefaults.standardUserDefaults().synchronize();

How can make it as a session so I can use it in every page

example Welcome "userLoggedin"

MuaathAli
  • 117
  • 4
  • 15

2 Answers2

5

use singleton maybe helpful:

class LoginInfo {

var isLogin:Bool = false
static let shareInstance = LoginInfo()

init() {}
}

// when login success set isLogin be true
LoginInfo.shareInstance.isLogin = true

// when login out set isLogin be false
LoginInfo.shareInstance.isLogin = false

// in other pages can call this
if LoginInfo.shareInstance.isLogin {
        // do something
}

hope it be helpful :-)

Wilson XJ
  • 1,750
  • 13
  • 14
4

For you store the id user, you use:

NSUserDefaults.standardUserDefaults().setObject(userId, forKey:"userId");
NSUserDefaults.standardUserDefaults().synchronize();

In other page:

let userId = NSUserDefaults.standardUserDefaults().objectForKey("userId") as? [String]
oremag14jf
  • 326
  • 3
  • 9