1

I have this class Signin which is initialized when the user signs in using username, password but it also gets and sets the uname, password to keychain. I'm going to use this Signin class everywhere else in the code but I dont want to create a new instance each time but as the user logs in for the first time, should be initialized and then throughout the app, that same instance should be used unless the user decides to sign out.

I declare and create a instance of this class in AppDelegate where my sign in code is.

var signin: SignIn?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

signin = SignIn(username: fullName!, password: "")

}

Here is how my class looks like:

class SignIn {
    let username: String
    let password: String
    let keychain = KeychainSwift()

    init(username: String, password: String) {
        self.username = username
        self.password = password
    }

    func getUsername() -> String {
        if (keychain.get("username") != nil) {
            return keychain.get("username")!
        }
        return ""
    }

    func setUsername(username: String) {
        keychain.set(username, forKey: "username")
    }

    func getPassword() -> String {
        if (keychain.get("password") != nil) {
            return keychain.get("password")!
        }
        return ""
    }

    func setPassword(password: String) {
        keychain.set(password, forKey: "password")
    }
}

But when I try to access it lets say in a ViewController to say "welcome username". I do something like this:

class ViewController: UIViewController {
    @IBOutlet weak var welcomeText: UITextField!
    var signIn: SignIn?

    override func viewDidLoad() {

        let username: String = signIn!.getUsername()
         welcomeText.text = "Welcome \(username)"
    }
}

Is this the right way? How do I make the class in such a way that I get a shared instance of the Signin class and that same instance gets shared everywhere and while signing out, I uninitialize the class.

fscore
  • 2,567
  • 7
  • 40
  • 74

3 Answers3

1

You can use singleton pattern, something like this:

class SignIn {
    static let sharedInstance = SignIn()

// your code
}

After this, you access your sharedInstance with

SignIn.sharedInstance

from everywhere in your code

Vladyslav Zavalykhatko
  • 15,202
  • 8
  • 65
  • 100
1

Try something like this:

class SignIn {
    let username: String
    let password: String
    static let sharedInstance = SignIn()
    let keychain = KeychainSwift()

    // Initialization

    private init() {
        self.username = keychain.get("username")!

        self.password = keychain.get("password")!

    }

}

On Login success, you've to set username and password in the keychain and call SignIn.sharedInstance, then afterward you can access the username and password from anywhere, something like this:

    let signIn = SignIn.sharedInstance
    print("Username ="+signIn.username)
    print("Password ="+signIn.password)

Hope it helps.

Imad Ali
  • 3,261
  • 1
  • 25
  • 33
-1
class User {

    static let sharedInstance = User()

    var name :String?
    var phoneNumber : String?



    func initializer(_ nm : String , ph : String){
        name = nm
        phoneNumber = ph
    }
}

Initialize it using

User.sharedInstance.initializer("Jen", ph: "2323")
Jen Jose
  • 3,995
  • 2
  • 19
  • 36