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,269評論 25 708
  • 吃貨地圖產品需求文檔 V1.0-2015/03/30 1概述 1.1產品概述及目標 概述:“吃貨地圖”是一款基于i...
    michaelshan閱讀 5,889評論 1 46
  • 2017.02.22 可以練習,每當這個時候,腦袋就犯困,我這腦袋真是神奇呀,一說讓你做事情,你就犯困,你可不要太...
    Carden閱讀 1,373評論 0 1
  • 時光機的碎片:《致吶喊》 高學海 年輕時曾做過許多的夢 在無端悲哀與反省的過程中 逐漸忘卻 回憶總是讓人歡欣 卻充...
    高學海閱讀 149評論 0 0
  • 農歷八月 -桂月 古人認為八月是桂樹的花季,所以農歷八月又被稱作桂月。這時候滿城的桂花開始悄悄吐露花蕊,再過幾日便...
    形色閱讀 1,380評論 0 5