登录后置 : 用户打开app不需要先登录,直接能进入app首页. 然后点击某些特定功能页面或者特定按钮提示登录页面.
我们需要解决的问题是如何有效的把登录触发加在按钮点击上, 当然我们可以在每个需要判断登录的按钮上判断, 不过这样做不太高效, 显得有点机械.
下面介绍下我的处理方式
- (UIButton *)messageBtn{
if (!_messageBtn) {
_messageBtn =[UIButton buttonWithType:UIButtonTypeCustom];
//是否需要判断登录状态
_messageBtn.needJudgeLoginStatus = YES;
//登录完以后是否接着出发按钮的点击
_messageBtn.continueOperationAfterLogin = YES;
_messageBtn.frame = CGRectMake(0, 0, 28, 28);
[_messageBtn setImage:[UIImage imageNamed:@"kefu"] forState:UIControlStateNormal];
[_messageBtn addTarget:self action:@selector(serviceBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _messageBtn;
}
以上是需要判断登录状态按钮的懒加载, 可以看到用到了
//是否需要判断登录状态
_messageBtn.needJudgeLoginStatus = YES;
//登录完以后是否接着出发按钮的点击
_messageBtn.continueOperationAfterLogin = YES;
这个就是我们封装的属性, 来添加是否需要登录提示, 很方便,只需要在指定的按钮上加上这个属性就能生效.
下面我们来看具体实现
- 写UIControl或者UIButton的分类 这样就可以添加我们的方法或者重写系统的方法来实现我们的需求
#import
@interface UIControl (JudgeLoginStatus)
@property (nonatomic, assign) BOOL needJudgeLoginStatus;
@property (nonatomic, assign) BOOL continueOperationAfterLogin;
//此block 是为了实现点击按钮完成登录以后我们接着要做的事
@property (nonatomic, copy) void(^loginSuccessBlock)();
@end
2.这里运用到了运行时的*关联( 关联是指把两个对象相互关联起来,使得其中的一个对象作为另外一个对象的一部分。)(可自行百度)
一下代码在.m文件下
#pragma mark - Getter and Setter
- (BOOL)needJudgeLoginStatus
{
NSNumber *numberValue = objc_getAssociatedObject(self, @selector(needJudgeLoginStatus));
return [numberValue boolValue];
}
- (void)setNeedJudgeLoginStatus:(BOOL)needJudgeLoginStatus
{
NSNumber *numberValue = [NSNumber numberWithBool:needJudgeLoginStatus];
objc_setAssociatedObject(self, @selector(needJudgeLoginStatus), numberValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)continueOperationAfterLogin
{
NSNumber *numberValue = objc_getAssociatedObject(self, @selector(continueOperationAfterLogin));
return [numberValue boolValue];
}
- (void)setContinueOperationAfterLogin:(BOOL)continueOperationAfterLogin
{
NSNumber *numberValue = [NSNumber numberWithBool:continueOperationAfterLogin];
objc_setAssociatedObject(self, @selector(continueOperationAfterLogin), numberValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void (^)())loginSuccessBlock
{
return objc_getAssociatedObject(self, @selector(loginSuccessBlock));
}
- (void)setLoginSuccessBlock:(void (^)())loginSuccessBlock
{
objc_setAssociatedObject(self, @selector(loginSuccessBlock), loginSuccessBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
3.因为我们要把我们的需求加在按钮的点击方法里面, 所以系统的方法不能满足我们, 我们要使用我们自己的方法替换系统的方法(方法交换/魔法交换/黑盒子)(可自行百度)
#pragma mark - Method swizzling
+ (void)load
{
SEL originSEL = @selector(sendAction:to:forEvent:);
SEL mySEL = @selector(my_sendAction:to:forEvent:);
Method originMethod = class_getInstanceMethod([self class], originSEL);
const char *typeEncodinds = method_getTypeEncoding(originMethod);
Method newMethod = class_getInstanceMethod([self class], mySEL);
IMP newIMP = method_getImplementation(newMethod);
if (class_addMethod([self class], mySEL, newIMP, typeEncodinds))
{
class_replaceMethod([self class], originSEL, newIMP, typeEncodinds);
}
else
{
method_exchangeImplementations(originMethod, newMethod);
}
}
- 实现自己的方法
- (void)my_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
//需要判断登录状态&&未登录
if (self.needJudgeLoginStatus && ![[YJJDataEngine shareEngine] isLogin])
{
YJJNewLoginViewController *vc = [[YJJNewLoginViewController alloc]init];
//登录成功的回调
vc.loginSuccessBlock = ^ () {
//继续做按钮本身要做的事
if (self.continueOperationAfterLogin)
{
[self my_sendAction:action to:target forEvent:event];
}
};
YJJLoginNavigationController *nav = [[YJJLoginNavigationController alloc] initWithRootViewController:vc];
UIViewController *parentVC = [self getCurrentViewController];
[parentVC presentViewController:nav animated:YES completion:nil];
}
else
{
[self my_sendAction:action to:target forEvent:event];
}
}
//
- 举个例子 : 首页有个按钮messageButton 它的点击事件是要进入消息页面, 但是进入之前要检测用户是否登录, 登录了直接就进入了, 未登录直接弹出登录页面, 输入账号密码实现登录操作,后直接进入消息页
这个时候就可以设置一下两个属性:
//是否需要判断登录状态
_messageBtn.needJudgeLoginStatus = YES;
//登录完以后是否接着出发按钮的点击
_messageBtn.continueOperationAfterLogin = YES;
完!!!