最近在項(xiàng)目中添加了登陸注冊(cè)模塊 把自己遇到的問(wèn)題和經(jīng)驗(yàn)分享一下
首先是登陸模塊
后臺(tái)約定登陸接口發(fā)起登陸請(qǐng)求時(shí)需要傳
參數(shù):
- ulogin:用戶名或手機(jī)號(hào)碼
- upassword:密碼明文
返回:
- status :0表示用戶被禁用或者用戶名密碼錯(cuò)誤
- status:1表示登陸成功并返回user數(shù)據(jù) token JSESSIONID
首先是界面
界面用xib搭建
登陸按鈕 默認(rèn)不可交互的
按鈕圓角可通過(guò)xib的runtime Attributes設(shè)置
密碼框設(shè)置為安全輸入
對(duì)兩個(gè)輸入框的輸入長(zhǎng)度要進(jìn)行判斷
系統(tǒng)提供的代理方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
不好使
對(duì)兩個(gè)輸入框進(jìn)行UIControlEventEditingChanged監(jiān)聽(tīng)
[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;
}
}
用戶名長(zhǎng)度大于1且密碼在6-20位之間時(shí)候(密碼大于20位就不可輸入)按鈕是可以交互的
設(shè)置三個(gè)標(biāo)志全局變量進(jìn)行判斷登錄按鈕是否可交互
BOOL allOK;
BOOL accountOK;
BOOL passwordOk;
當(dāng)用戶名和密碼都格式正確時(shí) 點(diǎn)擊登錄按鈕發(fā)去登錄請(qǐng)求
- (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:@"用戶名或者密碼錯(cuò)誤" 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];
}];
}
}
請(qǐng)求完畢后 服務(wù)器會(huì)給你緩存下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
>
我在靜態(tài)方法庫(kù)添加了一個(gè) 用于快速保存和取cookie到NSUserDefaults的方法方便存取和使用 因?yàn)楹竺娴拈_(kāi)發(fā)可能會(huì)使用到cookie
每次應(yīng)用啟動(dòng)時(shí)系統(tǒng)自己加載cookie會(huì)需要一定的時(shí)間
關(guān)于ios的Cookie那些事
在didFinishLaunchingWithOptions
方法里加載cookie
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
三個(gè)方法如下:
+(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];
}
}
登錄時(shí)要隱藏輸入鍵盤(pán) 讓鍵盤(pán)放棄第一響應(yīng)
- (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];
}