IOS uses token to keep landing status

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.