Nsuserdefault is an apple provided to us to store the user's login information.Persistence ofclass. The following example is used to see the process of logging the status of the log
First, create a method class for accessing user information
HRAccountTool.h:
#import <Foundation/Foundation.h> @interface hraccounttool:nsobject/** * Store account information * @param accounts Need to store account information: The first value is the user name, the second value is the password */+ (void) Saveaccount: (Nsarray *) account;/** * Returns the stored account information * @return Nsarray */+ (Nsarray * ) getaccount;/** * Returns the stored login user name * @return nsstring */+ (NSString *) getusername;/** * Returns the stored login user password * @ return nsstring */+ (NSString *) GetPassword; @end
Implementation class
HRACCOUNTTOOL.M:
<pre name= "code" class= "OBJC" > #import "HRAccountTool.h" @implementation hraccounttool+ (void) Saveaccount: ( Nsarray *) account{nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults]; Save data user information, user name, user password [userdefaults setobject:account forkey:@ "account"]; [Userdefaults setobject:[account objectatindex:0] forkey:@ "UserName"]; [Userdefaults setobject:[account objectatindex:1] forkey:@ "PassWord"];}; + (Nsarray *) getaccount{nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults]; Nsarray *array = [userdefaults objectforkey:@ "account"]; return array;}; + (NSString *) getusername{nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults]; NSString *username = [userdefaults objectforkey:@ "UserName"]; return userName;}; + (NSString *) getpassword{nsuserdefaults *userdefaults = [Nsuserdefaults standarduserdefaults]; NSString *password = [userdefaults objectforkey:@ "PassWord"]; return PassWord;} @end
InAPPDELEGATE.M Page Jump method, you can call to obtain user information/user name method to determine whether the need to log in. Do not need to go directly to the homepage, you need to enter the landing page:
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions { Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]]; Self.window.backgroundColor = [Uicolor whitecolor]; First determine if there is no storage account information if ([hraccounttool getusername] = = nil) {//before successful login self.window.rootViewController = [[ Hrloginviewcontroller alloc] init]; } else {///before login successful// Self.window.rootViewController = [[Iwoauthviewcontroller alloc] init]; } [Self.window makekeyandvisible]; return YES;}
If you enter the landing page in the login event to persist login user information toNsuserdefault, you can guarantee the next free landing:
-(void) logincheck{//1. Create Request Management Object Afhttprequestoperationmanager *mgr = [Afhttprequestoperationmanager manager]; Describes the JSON data returned by the server Mgr.responseserializer = [Afjsonresponseserializer serializer]; 2. Package request parameter Nsmutabledictionary *params = [Nsmutabledictionary dictionary]; params[@ "userName"] = Usernametf.text; params[@ "PassWord"] = Passwordtf.text; 3. Send request [Mgr get:@ "http://localhost:8080/login/check.do" Parameters:params success:^ (afhttprequestoperation *oper ation, Nsdictionary *responseobject) {if ([[Responseobject objectforkey:@ "Success"] boolvalue] = = True) { Self.view.window.rootViewController = [[Zytabbarviewcontroller alloc]init]; Nsarray *array = [Nsarray arrayWithObjects:userNameTF.text, Passwordtf.text, nil]; [Hraccounttool Saveaccount:array]; }else{Self.view.window.rootViewController = [[Zyloginviewcontroller alloc]init]; }} Failure:^ (Afhttprequestoperation *operation, Nserror *error) {Hrlog (@ "Request failed:%@", error); }]; }
IOS Record User Login status second time free login