How to maintain a session in objective-c?

后端 未结 4 1418
鱼传尺愫
鱼传尺愫 2020-12-29 16:45

So i got the following problem:

I got a login viewcontroller and a form viewcontroller. On the login viewcontroller, i am sending a POST request to a PHP script whic

4条回答
  •  抹茶落季
    2020-12-29 17:21

    I'm gonna give you a comprehensive answer, with Swift.

    Don't use NSUserDefaults and don't store password it's a bad solution

    NSUserDefaults data is not encrypted, it may cause security issue.

    Let's create a structured user class instead

    When the user logged in, you will need to make sure you have access to user data throughout the app so you can get the data on any screen when you need it.

    To achieve this, we need to make a great structure to organize this properly. Remember that current user and another users are both "user" so we will use the same class.

    Create a class and name it "EDUser" (you can choose other name if you want).
    This class will contain a user information (either current user or other user).
    More than that, this class will have capability to log the user in.

    Here's a picture of what the class might look like:

    class EDUser {
        var firstName: String
        var lastName: String?
        var birthDate: NSDate?
    
        init(firstName: String, lastName: String?, birthDate: NSDate?) {
            self.firstName = firstName
            self.lastName = lastName
            self.birthDate = birthDate
        }
    }
    
    // MARK: - Accessor
    
    extension EDUser {
        class var currentUser: EDUser? {
            get {
                return loadCurrentUserFromDisk()
            }
            set {
                saveCurrentUserToDiskWithUser(newValue)
            }
        }
    }
    
    // MARK: - Log in and out
    
    extension EDUser {
        class func loginWithUsername(username: String,
                               andPassword password: String,
                               callback: (EDUser?, NSError) -> Void) {
            // Access the web API
            var parameters = [
                "username": username,
                "password": password
            ]
            YourNetworkingLibrary.request(.POST,
                              "https://api.yourwebsite.com/login",
                              parameters: parameters).responseJSON { 
                response in
    
                if response.statusCode == .Success {
                    let user = EDUser(firstName: response["firstName"],
                           lastName: response["lastName"],
                           birthDate: NSDate.dateFromString(response["birthDate"]))
                    currentUser = user
                    callback(currentUser, nil)
                } else {
                    callback(nil, yourError)
                }
            }
        }
    
        class func logout() {
            deleteCurrentUserFromDisk()
        }
    }
    
    // MARK: - Data
    
    extension EDUser {
        class private func saveCurrentUserToDiskWithUser(user: EDUser) {
            // In this process, you encode the user to file and store it
        }
    
        class private func loadCurrentUserFromDisk() -> EDUser? {
            // In this process, you get the file and decode that to EDUser object
            // This function will return nil if the file is not exist
        }
    
        class private func deleteCurrentUserFromDisk() {
            // This will delete the current user file from disk
        }
    }
    
    // MARK: - Helper
    
    extension NSDate {
        class func dateFromString(string: String) -> NSDate {
            // convert string into NSDate
        }
    }
    

    Use Case

    Now with everything in place, we can use it like this

    Non-blocking logging in process

    EDUser.loginWithUsername(username: "edward@domain.com",
                             password: "1234") {
        user, error in
    
        if error == nil {
            // Login succeeded
        } else {
            // Login failed
        }
    }
    

    Logging out

    EDUser.logout()
    

    Check whether the user is logged in

    if EDUser.currentUser != nil {
        // The user is logged in
    } else {
        // No user logged in
        // Show the login screen here
    }
    

    Get current user data on any screen

    if let currentUser = EDUser.currentUser {
        // do something with current user data
    }
    

    Store other user as object

    let user = EDUser(firstName: "Edward",
                      lastName: "Anthony",
                      birthDate: NSDate())
    

提交回复
热议问题