我和另一位同事負責公司登錄與用戶中心模塊,排期兩週。扣掉和產品、後端對接的時間,再扣掉原型圖反覆修改期間的內耗,能在兩週內交付已經算幸運。事後回顧,這個模塊真正的難點不在功能本身,而在那些「產品不會寫、UE 想不到、但用戶一定會遇到」的邊界情況。
登錄註冊模塊內部的頁面高度相似:同樣的背景、同樣的導航欄樣式、同樣的返回與退出行為、同樣的輸入框與按鈕樣式。



與其把這些樣式在五個 ViewController 裡各寫一遍,不如抽一個父控制器。下面是我個人專案《丁丁印記》的登錄註冊目錄結構,HooEntryBaseViewController 就統一承載了整個模塊的通用行為與樣式:

子類只需覆寫差異部分,頁面搭建速度立刻快一倍以上。
這是開發者最容易忽略的一環。我因為鍵盤遮住輸入框而怒刪 App 過,相信不是少數。
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(closeKeyboard:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tap];
- (void)closeKeyboard:(id)sender
{
[self.view endEditing:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect frame = textField.frame;
// 216 為鍵盤高度
int offset = frame.origin.y + 120 - (self.view.frame.size.height - 216.0);
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
[UIView setAnimationDuration:animationDuration];
if (offset > 0) {
self.view.frame = CGRectMake(0.0f, -offset,
self.view.frame.size.width,
self.view.frame.size.height);
}
[UIView commitAnimations];
}
更現代的寫法是監聽 UIKeyboardWillShowNotification,從 userInfo 中取出鍵盤實際高度——各家輸入法的鍵盤高度並不相同,硬編碼 216 在第三方鍵盤上會出錯。
這條是血的教訓。接入第三方登錄時必須先判斷用戶是否安裝了該客戶端,否則蘋果審核很可能不通過。
《丁丁印記》接入了 QQ 登錄,程式會先檢查,未安裝就把入口隱藏:
if (![QQApi isQQInstalled]) {
self.QQLoginButton.hidden = YES;
self.QQLoginLabel.hidden = YES;
}
年輕的開發者往往不會主動想到這些,而產品與 UE 也常常忘了定義臨界行為。
- (IBAction)login:(UIButton *)sender
{
if ([self.userNameTextField.text isEmpty] ||
[self.passwordTextField.text isEmpty]) {
[SVProgressHUD showErrorWithStatus:@"用戶名或密碼不能為空"];
return;
}
__weak typeof(self) weakSelf = self;
[[HooUserManager manager]
LoginWithUserName:self.userNameTextField.text
andPassword:self.passwordTextField.text
block:^(BmobUser *user, NSError *error) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (error) {
[SVProgressHUD showErrorWithStatus:@"登錄失敗"];
} else if (user) {
[SVProgressHUD showSuccessWithStatus:@"登錄成功"];
[strongSelf loginSuccessDismiss];
}
}];
}
NSString *emailStr;
NSString *phoneStr;
NSString *passwordStr = weakSelf.passwordView.inputTextField.text;
emailStr = weakSelf.accountView.inputTextField.text;
if (![NSString validateEmail:emailStr] || !emailStr.length) {
[weakSelf showErrorTipViewWithMessage:@"郵箱格式錯誤"];
return;
} else {
phoneStr = weakSelf.accountView.inputTextField.text;
if (phoneStr.length < 5) {
[weakSelf showErrorTipViewWithMessage:@"手機長度錯誤"];
return;
}
if ([weakSelf.accountView.countryCode isEqualToString:@"+86"]) {
if (![phoneStr isValidateMobileNumber]) {
[weakSelf showErrorTipViewWithMessage:@"手機號碼格式錯誤"];
return;
}
}
}
if (passwordStr.length < kPasswordMinLength) {
[weakSelf showErrorTipViewWithMessage:@"密碼長度不能少於 6 個字符"];
return;
}
if (passwordStr.length > kPasswordMaxLength) {
[weakSelf showErrorTipViewWithMessage:@"密碼長度不能超過 20 個字符"];
return;
}
這個需求產品通常不會主動提,得開發者自己想到並去確認。否則用戶可以瘋狂點擊,既加重伺服器負擔,又會收到十幾封簡訊。下面這段用 GCD timer 實作了兩分鐘倒數,倒數期間按鈕不可點:
- (void)verifedCodeButtonWithTitle:(NSString *)title
andNewTitle:(NSString *)newTitle
{
WS(weakSelf);
__block int timeout = kTimeout;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(_timer, ^{
if (timeout <= 0) {
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf setTitle:title forState:UIControlStateNormal];
weakSelf.userInteractionEnabled = YES;
});
} else {
NSString *strTime = [NSString stringWithFormat:@"%.2d", timeout];
dispatch_async(dispatch_get_main_queue(), ^{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[weakSelf setTitle:[NSString stringWithFormat:@"%@(%@)", newTitle, strTime]
forState:UIControlStateNormal];
[UIView commitAnimations];
weakSelf.userInteractionEnabled = NO;
});
timeout--;
}
});
dispatch_resume(_timer);
}
切換到背景時記得暫停 timer,回前景再續算,否則使用者回來會發現倒數早就跑完了。
業務層通常會用單例管理登錄資料,但不能只依賴單例判斷登錄狀態——App 重啟後單例就清空。判斷依據應該是沙盒裡的使用者標識是否存在。
- (void)saveUserInfoWithData:(NSDictionary *)dict
{
NSString *userID = dict[kUserId];
NSString *email = dict[kEmail];
NSString *mobile = dict[kMobile];
[HooNSUserDefaultSerialzer setObject:userID forKey:kUserID];
[HooNSUserDefaultSerialzer setObject:email forKey:kEmail];
[HooNSUserDefaultSerialzer setObject:mobile forKey:kMobile];
}
這一點是架構層面的。如果每個頁面都自己去沙盒撈資料,遲早會演變成一場災難:欄位名拼錯、型別不一致、某處改了快取 key 其他地方卻沒同步。正確做法是封裝一個統一入口:
// HooUserManager.h #import <Foundation/Foundation.h> #import "HooSingleton.h" @interface HooUserManager : NSObject @property (nonatomic, strong) NSString *userID; SingletonH(Manager) /** 判斷是否已登錄 */ - (BOOL)isUserLogin; /** 登出 */ - (void)loginOut; @end
// HooUserManager.m
#import "HooUserManager.h"
#import "HooNSUserDefaultSerialzer.h"
static NSString * const kMobile = @"Mobile";
static NSString * const kEmail = @"Email";
static NSString * const kUserID = @"UserID";
@implementation HooUserManager
SingletonM(Manager)
- (NSString *)userID
{
return [HooNSUserDefaultSerialzer objectForKey:kUserID];
}
- (BOOL)isUserLogin
{
NSString *userID = [HooNSUserDefaultSerialzer objectForKey:kUserID];
return userID.length > 0;
}
- (void)loginOut
{
[HooNSUserDefaultSerialzer removeObjectForKey:kMobile];
[HooNSUserDefaultSerialzer removeObjectForKey:kEmail];
[HooNSUserDefaultSerialzer removeObjectForKey:kUserID];
}
@end
這樣一來,其他頁面想知道登錄狀態只需問 isUserLogin,需要唯一標識只需讀 userID,快取的實作細節被完全封裝。
為了更好的體驗,通常還會補上:密碼明文顯示切換按鈕、從伺服器動態拉取郵箱格式提示、常見錯誤輸入的自動糾正,以及最重要的——切換頁面時的過場動畫。這些都不是需求文檔上會寫的東西,但決定了一個登錄模塊讀起來是「能用」還是「好用」。