最近在項目中添加了登陸注冊模塊 把自己遇到的問題和經驗分享一下
首先是登陸模塊
后臺約定登陸接口發起登陸請求時需要傳
參數:
- 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];
}