iOS用宏封裝登錄頁面的跳轉(zhuǎn)邏輯

App在用戶執(zhí)行需要登錄權(quán)限的操作時候,往往需要先進行登錄判斷,如果未登錄,則直接跳轉(zhuǎn)到登錄頁面,否則直接進入業(yè)務(wù)邏輯,代碼大致如下:

- (void)actionGift {
    if ([NGCommonAPI isLogin]) {
        //已登錄,進入禮包頁面
        NGMyGiftVController *viewController = [NGMyGiftVController new];
        [self.navigationController pushViewController:viewController animated:YES];
    }else {
        //未登錄,跳轉(zhuǎn)到登錄頁面
        NGLoginViewController *login = [[NGLoginViewController alloc] init];
        [self presentViewController:[[UINavigationController alloc] initWithRootViewController:login] animated:YES completion:nil];
    }
}

其他地方需要登錄邏輯判斷的,也都要添加同樣的邏輯代碼,這樣存在幾個問題

  • 跳轉(zhuǎn)登錄的一套相同代碼存在程序的多處地方,違反了封裝的原則
  • if/else的寫法具有迷惑性,因為非登錄情況下,用戶除了跳轉(zhuǎn)到登錄頁面外,還有可能是其他的操作,不利于閱讀
  • presentViewController函數(shù)依賴于當(dāng)前頁面,在非viewController地方使用不具有通用性
Python Flask 路由實現(xiàn)

由于用Flask開發(fā)過后臺接口,發(fā)現(xiàn)其對需要登錄的處理十分優(yōu)雅,大致如下:

@admin.route('/h5_activity_prizes', methods=['GET'])
@login_required
def h5_activity_prizes():
    ur"""h5活動獎品設(shè)置"""
    g.uri_path = request.path
    ....

這里的login_required是python里的裝飾器,如果該接口是需要登錄權(quán)限的,則用該裝飾器修飾,否則不寫。登錄的判斷邏輯,以及未登錄的處理都封裝在里面,使用者無需知道內(nèi)部的實現(xiàn)邏輯。是不是很簡單?但是iOS并沒有裝飾器這樣的神器,試試用宏來實現(xiàn)看看

iOS 宏封裝登錄邏輯
  1. 新建一個GFCommonFun的通用類,封裝登錄校驗函數(shù),這里登錄頁面的彈出依賴于rootViewController
+ (UIViewController *)rootViewController{
    return [UIApplication sharedApplication].keyWindow.rootViewController;
}

+ (BOOL)checkLogin {
    if ([GFCommonFun isLogin]) {
        return NO;
    }
    UIViewController* root = [GFCommonFun rootViewController];
    GFLoginVController *viewController = [[GFLoginVController alloc] init];
    [root presentViewController:[[UINavigationController alloc] initWithRootViewController:viewController] animated:YES completion:^{
    }];
    return YES;
}
  1. 編寫宏,注意這里使用return控制跳轉(zhuǎn),在宏展開后,會跳出宏當(dāng)前所在的函數(shù)體,這樣實現(xiàn)了跳轉(zhuǎn)到登錄頁面后而無需執(zhí)行剩余代碼的功能
#define GF_Check_Login if([GFCommonFun checkLogin]) {return;};

3.最后在任意需要登錄權(quán)限的函數(shù)里第一行加上GF_Check_Login即可

#import "GFCommonFun.h"

//評論點贊
- (void)lightenComment:(NSString *)args {
    GF_Check_Login
    //具體點贊邏輯
    ...
}

基于同樣的原理,app中需要在函數(shù)執(zhí)行前的操作均可以封裝成宏的方式

備注
 上述方案并非最佳方案,有更好的方案,請指教
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容