最近在项目中添加了登陆注册模块 把自己遇到的问题和经验分享一下
首先是登陆模块
后台约定登陆接口发起登陆请求时需要传
参数:
- 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];
}