登陆Demo--使用UserDeaults实现自动登录,传值,了解数据持久化方法

来源:互联网 发布:web安全编程 编辑:程序博客网 时间:2024/06/05 09:24

要实现登陆demo的自动保存登录信息,登录后显示用户名。

页面间传值的方法有:属性传值,Block传值,代理传值,单例传值,通知传值等,属性列表里,用UserDefaults进行存值和取值,是能比较简单快速的保存轻量级本地数据的方法,比如登录界面的数据,用户名,密码之类的,下次再登录的时候就可以直接从NSUserDefaults里面读取上次登录的信息。它不需要用户在程序中设置UserDefaults的全局变量,需要在哪里使用NSUserDefaults的数据,就在哪里创建一个NSUserDefaults的对象,进行读写操作。

1/UISwitch开关选择是否保存用户名和密码

2/点击登录按钮,保存配置信息

3/申明一个Bool变量“isLogin"判断是否是第一次登录,是第一次登录显示框显示提示信息,非第一次登录显示读取的配置信息

1.ViewController.m的关键代码:

#import "ViewController.h"

#import "RightViewController.h"

#import "NewViewController.h"


@interface ViewController ()


@end


@implementation ViewController


-(void)loadView{

    [super loadView];

    NSLog(@"Hello");

}


- (void)viewDidLoad {

    [superviewDidLoad];

    

    

    [selfinitViews];

    

    //   [self readNSUserDefaults];  //调用此方法从NSUserDefautls中读取各种数据,在下面定义

    //存的是bool值一定要用boolForKey:获取

    //如果之前已经保存了UserDefaults,那么在读取的时候就要将数据读取出来,显示在用户名和密码框中

      NSUserDefaults *userDefaults = [NSUserDefaultsstandardUserDefaults];

     if ([userDefaults boolForKey:@"isLogin"] == YES) {

         

         UITextField *usernameTextField=(UITextField *)[self.viewviewWithTag:100];

         UITextField *passwordTextField=(UITextField *)[self.viewviewWithTag:101];


     usernameTextField.text = [userDefaults objectForKey:@"name"];

     passwordTextField.text = [userDefaults objectForKey:@"password"];

     }

     else

     {

         UITextField *usernameTextField=(UITextField *)[self.viewviewWithTag:100];

         UITextField *passwordTextField=(UITextField *)[self.viewviewWithTag:101];


     [usernameTextField setPlaceholder:@"请输入用户名"];

     [passwordTextField setPlaceholder:@"请输入密码"];

     }

    

}


-(void)initViews

{

    UILabel *label=[[UILabelalloc] initWithFrame:CGRectMake(100,100, 320, 40)];

    label.text=@"用户名:";

    [self.viewaddSubview:label];

    

    UILabel *label2=[[UILabelalloc] initWithFrame:CGRectMake(100,150, 320, 40)];

    label2.text=@"密码:";

    [self.viewaddSubview:label2];

    

    UILabel *label3=[[UILabelalloc] initWithFrame:CGRectMake(180,195, 100, 40)];

    label3.text=@"记住密码";

    label3.textColor=[UIColorblueColor];

    [self.viewaddSubview:label3];

    

    UITextField *usernameTextField=[[UITextFieldalloc] initWithFrame:CGRectMake(165,100, 150, 40)];

    usernameTextField.keyboardType=UIKeyboardTypeNamePhonePad;

    usernameTextField.borderStyle=UITextBorderStyleRoundedRect;

    usernameTextField.clearButtonMode=UITextFieldViewModeWhileEditing;

    // textfield.placeholder=@"请输入用户名";

    usernameTextField.secureTextEntry=NO;

    usernameTextField.textColor=[UIColorblackColor];

    usernameTextField.tag=100;//定义tag值,在后面用tag传递usernameTextField.text

                              //也可以把usernameTextField写成全局变量

    [self.viewaddSubview:usernameTextField];

    

    UITextField *passwordTextField=[[UITextFieldalloc] initWithFrame:CGRectMake(165,150, 150, 40)];

    passwordTextField.keyboardType=UIKeyboardTypeNamePhonePad;

    passwordTextField.borderStyle=UITextBorderStyleRoundedRect;

    passwordTextField.clearButtonMode=UITextFieldViewModeWhileEditing;

    // textfield2.placeholder=@"请输入密码";

    passwordTextField.secureTextEntry=YES;

    passwordTextField.textColor=[UIColorblackColor];

    passwordTextField.tag=101;//定义tag值,在后面用tag传递passwordTextField.text

                              //也可以把passwordTextField写成全局变量

    [self.viewaddSubview:passwordTextField];

    

    UIButton *loginButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    loginButton.frame=CGRectMake(100,240, 200, 40);

    [loginButton setTitle:@"登录"forState:UIControlStateNormal];

    [loginButton setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    [loginButton addTarget:selfaction:@selector(jumpSuccess)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:loginButton];

    

    UIButton *registerButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    registerButton.frame=CGRectMake(100,280, 200, 40);

    [registerButton setTitle:@"注册"forState:UIControlStateNormal];

    [registerButton setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    [registerButton addTarget:selfaction:@selector(jumpNew)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:registerButton];

    

    UISwitch *rembSwitch = [[UISwitchalloc] initWithFrame:CGRectMake(260,200, 100, 40)];

    rembSwitch.on=YES;

    [rembSwitch addTarget:selfaction:@selector(onSwitch:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:rembSwitch];


}

- (void)onSwitch:(id)sender {

 

    UISwitch *rembSwitch = sender;

    NSUserDefaults *userDefaults = [NSUserDefaultsstandardUserDefaults];

//点击开关,变化开关状态选择是否保存密码

    [userDefaults setBool:rembSwitch.onforKey: @"saveLogin"];

    [userDefaults synchronize];

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



-(void)jumpSuccess{

    RightViewController *rightvc=[[RightViewControlleralloc]init];

    rightvc.modalTransitionStyle=UIModalTransitionStyleCoverVertical;

    [selfpresentViewController:rightvc animated:YEScompletion:^{

    }];

    

        //   [self saveNSUserDefaults];  //调用此方法将各种数据存储到NSUserDefautls中,在下面定义

    NSUserDefaults *userDefaults = [NSUserDefaultsstandardUserDefaults];

    if ([userDefaults boolForKey:@"saveLogin"] == YES)

    {

        NSLog(@"1");

        UITextField *usernameTextField=(UITextField *)[self.viewviewWithTag:100];

        UITextField *passwordTextField=(UITextField *)[self.viewviewWithTag:101];

       [userDefaults setObject:usernameTextField.textforKey:@"name"];

       [userDefaults setObject:passwordTextField.textforKey:@"password"];

        [userDefaults setBool:YESforKey:@"isLogin"];

        [userDefaults synchronize];        

    }

    else {

        [userDefaults setBool:NOforKey:@"saveLogin"];

        NSLog(@"1-1");

    }


    }


-(void)jumpNew{

    NewViewController *newvc=[[NewViewControlleralloc]init];

    newvc.modalTransitionStyle=UIModalTransitionStyleCoverVertical;

    [selfpresentViewController:newvc animated:YEScompletion:^{

        

    }];

}


/*-(void)viewDidAppear:(BOOL)animated{

 

}*/

@end


2.RightViewController.m的关键代码:

#import "ViewController.h"

#import "RightViewController.h"


@interface RightViewController ()


@end


@implementation RightViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view from its nib.

    

    UILabel *nameString=[[UILabelalloc] initWithFrame:CGRectMake(170,150, 320, 40)];

    nameString.backgroundColor=[UIColorclearColor];

    nameString.textColor=[UIColorredColor];

    nameString.text=@"hello,";

    //UserDefaults里读取"name"的数据到"namestring"Label显示

    NSUserDefaults *userDefaults = [NSUserDefaultsstandardUserDefaults];

    nameString.text=[userDefaults objectForKey:@"name"];

    [self.viewaddSubview:nameString];

  

    UIButton *okButton=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    okButton.frame=CGRectMake(150,250, 100, 40);

    [okButton setTitle:@"OK"forState:UIControlStateNormal];

    [okButton addTarget:selfaction:@selector(close)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:okButton];

    

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


-(void)close{

    [selfdismissViewControllerAnimated:YEScompletion:^{

        

    }];

}

@end


0 0