iOS 登录界面

最近在项目中添加了登陆注册模块 把自己遇到的问题和经验分享一下

首先是登陆模块

后台约定登陆接口发起登陆请求时需要传
参数:

  • ulogin:用户名或手机号码
  • upassword:密码明文

返回:

  • status :0表示用户被禁用或者用户名密码错误
  • status:1表示登陆成功并返回user数据 token JSESSIONID

首先是界面


登陆界面

界面用xib搭建
登陆按钮 默认不可交互的
按钮圆角可通过xib的runtime Attributes设置


圆形按钮

密码框设置为安全输入

对两个输入框的输入长度要进行判断

系统提供的代理方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text

不好使
对两个输入框进行UIControlEventEditingChanged监听

[self.accountInput addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[self.passwordInput addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
-(void)textFieldDidChange:(UITextField * )textField{
    if (textField == _accountInput) {
        accountOK = NO;
        if(textField.text.length > 1) accountOK = YES;
    }
    if (textField == _passwordInput) {
        if(textField.text.length >= 6 && textField.text.length <= 20)passwordOk = YES;
        else passwordOk = NO;
        if (textField.text.length >= 20) {
            textField.text = [textField.text substringToIndex:20];
        }
    }
    if(accountOK&&passwordOk){
        _loginBtu.enabled = YES;
        allOK = YES;
    }else{
        _loginBtu.enabled = NO;
         allOK = NO;
    }
}

用户名长度大于1且密码在6-20位之间时候(密码大于20位就不可输入)按钮是可以交互的
设置三个标志全局变量进行判断登录按钮是否可交互

BOOL allOK;
BOOL accountOK;
BOOL passwordOk;

当用户名和密码都格式正确时 点击登录按钮发去登录请求

- (IBAction)loginAction:(id)sender {
    if(allOK){
        _hud = [[MBProgressHUD alloc]init];
        _hud.labelText = @"正在登录...";
        [self.navigationController.view addSubview:_hud];
        [_hud show:YES];
        [HBNetRequest Post:LOGIN para:@{
                                        @"ulogin"        :_accountInput.text,
                                        @"upassword"     :_passwordInput.text }
                  complete:^(id data) {
                      NSUInteger status = [data[@"status"] integerValue];
                      if (status==0) {
                          [self.view makeToast:@"用户名或者密码错误" duration:1.0 position:CSToastPositionCenter];
                          [_hud hide:YES];
                      }
                      if (status == 1) {
                          NSDictionary *userDic = data[@"user"];
                          HBUserItem *user = [[HBUserItem alloc] initWithDictionary:userDic error:nil];
                          [HBUserItem saveUser:user];
                          [HBAuxiliary saveCookie];

                          [_hud hide:YES];
                          [self hiddenKeyboardForTap];
                          
                          AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
                          appDelegate.window.rootViewController = [MainViewController new];
                      }
                  } fail:^(NSError *error) {
                      [_hud hide:YES];
                  }];
    }
 
}

请求完毕后 服务器会给你缓存下cookie 里面包含了一些信息
我把它打印输出

<NSHTTPCookie 
version:0 
name:"JSESSIONID" 
value:"9BE362C1ACCB6D82B3B6551F97C60F09" 
expiresDate:(null) 
created:2017-03-27 04:38:54 +0000 
sessionOnly:TRUE 
domain:"172.16.120.65" 
partition:"none" 
path:"/carshop/" 
isSecure:FALSE
>

我在静态方法库添加了一个 用于快速保存和取cookie到NSUserDefaults的方法方便存取和使用 因为后面的开发可能会使用到cookie

每次应用启动时系统自己加载cookie会需要一定的时间
关于ios的Cookie那些事

didFinishLaunchingWithOptions方法里加载cookie

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

三个方法如下:

+(void)saveCookie{
    NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject: [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject: cookiesData forKey: @"sessionCookies"];
    [defaults synchronize];
}


+(NSHTTPCookieStorage*)getCookies{
    NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @"sessionCookies"]];
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    
    for (NSHTTPCookie *cookie in cookies){
        [cookieStorage setCookie: cookie];
    }
    return cookieStorage;
}


+(void)loadCookies{
    NSArray *cookies = [NSKeyedUnarchiver unarchiveObjectWithData: [[NSUserDefaults standardUserDefaults] objectForKey: @"sessionCookies"]];
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    
    for (NSHTTPCookie *cookie in cookies){
        [cookieStorage setCookie: cookie];
    }
}

登录时要隐藏输入键盘 让键盘放弃第一响应

- (void)hiddenKeyboardForTap{
    [_accountInput resignFirstResponder];
    [_passwordInput resignFirstResponder];
}

左上角添加返回按钮

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backMainController)];

回到主界面 登录完成后一样

- (void)backMainController{
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    appDelegate.window.rootViewController = [MainViewController new];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,843评论 25 709
  • 吃货地图产品需求文档 V1.0-2015/03/30 1概述 1.1产品概述及目标 概述:“吃货地图”是一款基于i...
    michaelshan阅读 5,900评论 1 46
  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,388评论 0 1
  • 时光机的碎片:《致呐喊》 高学海 年轻时曾做过许多的梦 在无端悲哀与反省的过程中 逐渐忘却 回忆总是让人欢欣 却充...
    高学海阅读 150评论 0 0
  • 农历八月 ­桂月 古人认为八月是桂树的花季,所以农历八月又被称作桂月。这时候满城的桂花开始悄悄吐露花蕊,再过几日便...
    形色阅读 1,383评论 0 5