In the project, you need to stay logged in after the customer has landed. Use is not stored username and password, every time to simulate landing.
But log in, login successfully, receive Token,token from the server is a server generated a string, uniquely identify a user, and will be destroyed after a period of time, so we do not log in to the app, will require login again.
Use a tool class to store, delete, and read token. The login was successfully stored, the next time the app was opened for reading, and the exit was deleted.
If, have Refreshtoken, can carry on with new, do not need to log in again.
The code is as follows:
#import "GCTokenManager.h" nsstring *const Token_key = @ "TOKEN"; @implementation Gctokenmanager//Storage token + (void) Savetoken: (Gctoken *) token {nsuserdefaults *userdefaults=[nsuserdef
Aults Standarduserdefaults];
NSData *tokendata = [Nskeyedarchiver Archiveddatawithrootobject:token];
[Userdefaults Setobject:tokendata Forkey:token_key];
[Userdefaults Synchronize];
//Read token + (Gctoken *) GetToken {nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults];
NSData *tokendata = [Userdefaults Objectforkey:token_key];
Gctoken *token = [Nskeyedunarchiver unarchiveobjectwithdata:tokendata];
[Userdefaults Synchronize];
return token;
}//Empty token + (void) Cleantoken {nsuserdefaults *userloginstate = [Nsuserdefaults standarduserdefaults];
[Userloginstate Removeobjectforkey:token_key];
[Userloginstate Synchronize]; }//With new token + (Gctoken *) Refreshtoken {return nil;}
The Gctoken here is a model of the token returned by the server side, customized according to the JSON returned in its own project.