简体   繁体   中英

Manage User Logged in IOS App

I'm a junior IOS developer, i've find hard to understand some aspects of user management in a IOS APP.

I've already added facebook and google+ to my LoginView:

https://drive.google.com/file/d/0B_3MBkUrHOuRS2FEQW9rWUZkUkU/view?usp=sharing

Facebook and Google send data back to my app.

What should i do to access user data in other views. How to manage all this user data through my app.

Hope you can help Regards ADS

btw: I'm used to work in swift but i can interpret OBJ-C

1º Edit:------------------------------------------------------------------------

Changed my login view to:

 //MARK: Facebook Delegate Methods

func loginViewShowingLoggedInUser(loginView : FBLoginView!) {
    println("User Logged In")
}

func loginViewFetchedUserInfo(loginView : FBLoginView!, user: FBGraphUser) {
    // Store Facebook User Id
    userAccountManager.setUserId(user.objectForKey("id") as String)
    //Store Facebook User Name
    userAccountManager.setUserName(user.name)
    //Store Facebook E-mail account
    userAccountManager.setUserMail(user.objectForKey("email") as String)
    //MARK: FACEBOOK Session Token -------------------------------------------------------------<
    userAccountManager.setUserToken(FBSession.activeSession().accessTokenData.accessToken)

    userAccountManager.setUserLoggedIn(true)
}

func loginViewShowingLoggedOutUser(loginView : FBLoginView!) {
    println("User Logged Out")
}

func loginView(loginView : FBLoginView!, handleError:NSError) {
    println("Error: \(handleError.localizedDescription)")
}

//MARK: Google+ Methods

func finishedWithAuth(auth: GTMOAuth2Authentication!, error: NSError!) {
   storeUserData()
}


func storeUserData(){
    //Storing Google userId
    userAccountManager.setUserId(signIn?.userID as String!)
    //Storing Google User Name
    userAccountManager.setUserName(signIn?.googlePlusUser.valueForKey("displayName") as String)
    //Soting google e-mail account
    userAccountManager.setUserMail(signIn?.userEmail as String!)
    //MARK: GOOGLE Session Token -------------------------------------------------------------<
    userAccountManager.setUserToken(signIn?.idToken as String!)

    userAccountManager.setUserLoggedIn(true)
}

func didDisconnectWithError(error: NSError!) {
    println("Google Error: \(error)")
}

And stored the data in Account Manager.

class AccountManager{

var userDefaults = NSUserDefaults()


func setUserId(userId : String){
    userDefaults.setValue(userId, forKey: "USERID_KEY")
}

func setUserLoggedIn(loggedIn : Bool){
   userDefaults.setBool(loggedIn, forKey: "USER_LOGGED_IN_KEY")
}

func setUserName(userName : String){
    userDefaults.setValue(userName, forKey: "USERNAME_KEY")
}

func setUserMail(userMail : String){
    userDefaults.setValue(userMail, forKey: "USERMAIL_KEY")
}

func setUserToken(userToken :String){
    userDefaults.setValue(userToken, forKey: "USERTOKEN_KEY")
}

func getUserToken() -> String{
    return userDefaults.valueForKey("USERTOKEN_KEY") as String
}

func isUserLoggedIn() -> Bool{
    return userDefaults.valueForKey("USER_LOGGED_IN_KEY") as Bool
}

func getUserName() -> String {
    return userDefaults.valueForKey("USERNAME_KEY") as String
}

func getUserMail() -> String {
    return userDefaults.valueForKey("USERMAIL_KEY") as String
}
func getUserID() -> String {
    return userDefaults.valueForKey("USERID_KEY") as String
}

Am i supose to pass by segue the account manager object anytime i need user data?

最好的办法是NSUserDefaults的使用保存用户数据,这样就可以非常容易地调用这些数据下面是一个简短的介绍

The way I've done this in the past is create an AccountManager object that handles storing/retrieving all the relevant user params.

Under the covers this AccountManager object could use NSUserDefaults to store and retrieve account data, such as username, login status, etc...

A simple example would be something like this:

+ (void)setUserLoggedIn:(BOOL)loggedIn {
    [[NSUserDefaults standardUserDefaults] setBool:loggedIn forKey:@"USER_LOGGED_IN_KEY"];
}

+ (BOOL)isUserLoggedIn {
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"USER_LOGGED_IN_KEY"];
}

+ (void)setUsername:(NSString *)username {
    [[NSUserDefaults standardUserDefaults] setValue:username forKey:@"USERNAME_KEY"];
}

+ (NSString *)getUsername {
    return [[NSUserDefaults standardUserDefaults] valueForKey:@"USERNAME_KEY"];
}

Using the above example, in your login screen when the user successfully logs in, you could call [AccountManager setUserLoggedIn:YES]; , or if they later log out you can call [AccountManager setUserLoggedIn:NO];

Lets say there's some view that might change depending on if the user is logged in or not, you can easily determine this by calling [AccountManager isUserLoggedIn]; and format it accordingly.

The benefit of having an object that handles all of this stuff, rather than accessing NSUserDefaults values in all your views, is that you have a centralized place where all of the data is set and returned, so if anything changes you only need to change it in one place. Version 1 of your app might use emails only to sign up/log in, whereas Version 2 might use emails, facebook, google, etc., and you might need to tweak what values you're saving for those depending on your needs - but you'd only have to change it in that one class.

Also worth noting that if you're storing passwords, you shouldn't use NSUserDefaults, but rather use the Keychain, which a quick Google search would undoubtedly give you lots of tutorials/info on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM