登录状态的判定与处理

2018-10-30  本文已影响125人  春暖花已开

很多时候,跳转到下一个控制器或者响应某个事件之前,需要判断登录状态。如果没有登录,就调起登录控制器,完成登录之后,再接着响应之前的事件;如果登录了,直接响应之前的事件。那么,这个分类就应运而生了。

#import <UIKit/UIKit.h>

@interface UINavigationController (LoginPush)

/**
 适用于:
 跳转前需要检查登录状态,并且处于登录状态或登录成功后由`自己`处理的情况
 
 说明:目前逻辑是当用户处于未登录状态,但点击了需要登录才能跳转的页面时,会先跳转到登录页面;
 如果登录不成功会停留在登录页面,用户可以点击“关闭”来返回当前页面,故只存在登录成功回调
 */
- (void)beforePushCheckLoginStatusWithSuccess:(void (^)(void))successBlock;

@end
#import "UINavigationController+MZExtension.h"

#import <MZAccountManager/MZAccountManager.h>
#import <objc/runtime.h>

/** 监听的对象 */
static NSString *const kToken = @"token";
/** 登录控制器的类名 */
static NSString *const kLoginVCClassName = @"LoginViewController";

@interface UINavigationController ()

@property (nonatomic, strong) UIViewController *loginVC;
@property (nonatomic, copy) void (^successBlock)(void);

@end

@implementation UINavigationController (MZExtension)

- (void)beforePushCheckLoginStatusWithSuccess:(void (^)(void))successBlock {
    
    self.successBlock = successBlock;
    
    if (MZUserManager.token == nil || MZUserManager.token.length == 0 || [MZUserManager.token isEqualToString:@"##"]) {
        self.loginVC = [[NSClassFromString(kLoginVCClassName) alloc] init];
        
        if (self.loginVC) {
            [MZUserManager addObserver:self forKeyPath:kToken options:NSKeyValueObservingOptionNew context:nil];
            
            UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:self.loginVC];
            [self presentViewController:navVC animated:YES completion:nil];
            self.loginVC = nil;
        }
    } else {
        !self.successBlock ?: self.successBlock();
        self.successBlock = nil;
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    
    if ([keyPath isEqualToString:kToken]) {
        if (![self isEmpty:change[NSKeyValueChangeNewKey]] && ![change[NSKeyValueChangeNewKey] isEqualToString:@"##"]) {
            !self.successBlock ?: self.successBlock();
        }
    }
    [MZUserManager removeObserver:self forKeyPath:kToken];
    self.successBlock = nil;
}

- (UIViewController *)loginVC {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setLoginVC:(UIViewController *)loginVC {
    objc_setAssociatedObject(self, @selector(loginVC), loginVC, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void (^)(void))successBlock {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setSuccessBlock:(void (^)(void))successBlock {
    objc_setAssociatedObject(self, @selector(successBlock), successBlock, OBJC_ASSOCIATION_COPY);
}

- (BOOL)isEmpty:(NSString *)input {
    return [input isKindOfClass:[NSNull class]] || input == nil || [input length] <= 0;
}

@end

#import "LoginViewController.h"

#import <MZAccountManager/MZAccountManager.h>

@interface LoginViewController ()

@property (nonatomic, strong) NSNumber *kLoginStatus;

@end

@implementation LoginViewController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"%@创建了", [self.class description]);
}


#pragma mark - Public Methods

- (IBAction)onClickToLogin:(id)sender {
    
    NSLog(@"点击登录");
    
    MZUserManager.token = @"已经登录了";
    
    [self dismissLoginVC];
}

- (IBAction)onClickToClose:(id)sender {
    
    MZUserManager.token = @"##";
    [self dismissLoginVC];
}

- (void)dismissLoginVC {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - Private Methods

- (void)dealloc {
    NSLog(@"%@释放了", [self.class description]);
}

@end

上一篇下一篇

猜你喜欢

热点阅读