iOS引导登录、登录引导、login(点击某个功能按钮需要登录并且登录完成后自动跳转到要去的功能实现方法)

我们要实现的功能是:

1.未登录的情况下,点击某个按钮进入到不需要登录的功能,则直接进入

2.未登录的情况下,点击某个按钮进入到需要登录的功能A,则先进入登录界面,登录成功后直接跳转到这个功能A,登录不成功不跳转

3.已经登录的情况下,点击某个按钮进入到需要登录的功能A,则直接进入

 

实现这个功能有简单版的(重复代码会多,容易理解),稍复杂版的(简单版的封装,重复代码少,逻辑稍微有点复杂)

简单版目录截图:

 

说明:

1是三个功能页面,分别为转账,汇款,借款

2是登录界面,包括:用户名输入框,密码输入框,登录按钮,取消按钮

3是AppDelegate  设置TabbarController

4是TabbarController的viewControllers元素,页面包括三个圆按钮

5是 写宏定义的文件 和 frame的方便调用方法

6是按钮图标

简单实现的demo地址是:

https://github.com/XiaoHeHe1/GuideLoginDemoSimple

 

下面说实现过程,只贴关键代码,

1.用一个全局变量记录是否登录 ,点击一个按钮时,进行判断,如果没有登录则往NSNotificationCenter添加一个观察者和名字然后展示登录界面

 

//三个按钮的点击事件
- (void)btnClicked:(UIButton *)btnn{

    if (btnn.tag == 0) {
        //
        //判断是否登录 登录了直接进 没登录引导登录并登录成功后直接跳到对应业务
        //
        if (APPLICATION.isLogin == YES) {
            [self jumpToZhuanZhang];
        }else{
            //
            //先移除本通知,原因是:点击一个功能跳到登录界面,但是在登录界面点的取消,反复操作,再点这个功能,
            //相同的通知会增加多次,登陆成功后会多次进入相应的功能
            //
            [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LOGIN_ZZ" object:nil] ;
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpToZhuanZhang) name:@"LOGIN_ZZ" object:nil];
            LoginViewController *loginVC = [[LoginViewController alloc]init];
            [self.navigationController presentViewController:loginVC animated:YES completion:^{}];
        }
    }else if(btnn.tag == 1){
        if (APPLICATION.isLogin == YES) {
            [self jumpToHuiKuan];
        }else{

            [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LOGIN_HK" object:nil] ;
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpToHuiKuan) name:@"LOGIN_HK" object:nil];
            LoginViewController *loginVC = [[LoginViewController alloc]init];
            [self.navigationController presentViewController:loginVC animated:YES completion:^{
                
            }];
        }

    }else{
        if (APPLICATION.isLogin == YES) {
            [self jumpToJieKuan];
        }else{
            
            [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LOGIN_JK" object:nil] ;
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpToJieKuan) name:@"LOGIN_JK" object:nil];
            LoginViewController *loginVC = [[LoginViewController alloc]init];
            [self.navigationController presentViewController:loginVC animated:YES completion:^{
                
            }];
        }

    }
    
}
-(void)jumpToZhuanZhang{
    ZhuanZhangViewController *vc = [[ZhuanZhangViewController alloc]init];
    vc.hidesBottomBarWhenPushed =YES;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)jumpToHuiKuan{
    HuiKuanViewController *vc = [[HuiKuanViewController alloc]init];
    vc.hidesBottomBarWhenPushed =YES;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)jumpToJieKuan{
    JieKuanViewController *vc = [[JieKuanViewController alloc]init];
    vc.hidesBottomBarWhenPushed =YES;
    [self.navigationController pushViewController:vc animated:YES];
}
 

 

2.登录验证成功后,去掉登录页面,并发送通知执行。不成功则在当前页t停留
 

 

- (IBAction)loginBtnClicked:(id)sender {
    
    if (self.userName.text.length > 0 && self.passWord.text.length > 0) {

        APPLICATION.isLogin = YES;
        
        [self dismissViewControllerAnimated:YES completion:^{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LOGIN_ZZ" object:nil ];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LOGIN_HK" object:nil ];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LOGIN_JK" object:nil ];
            
        }];
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"输入有误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
}

 

3.这样基本就实现了,但是有问题。依次点击转账按钮---->出现登录界面--->点取消--->登录界面消失--->点汇款按钮---->出现登录界面--->输入用户名密码点登录。结果是依次进入了转账页面和汇款页面。修改这个问题需要增加一个enum记录引导登录的功能。代码更改为:

 

********声明********


typedef enum JumpType{ ZHUANZHANG_HOME_LOGIN = 0, HUIKUAN_HOME_LOGIN , JIEKUAN_HOME_LOGIN , }MYJumpType;


********转账按钮点击事件********

    if (btnn.tag == 0) {
        //
        //判断是否登录 登录了直接进 没登录引导登录并登录成功后直接跳到对应业务
        //
        if (APPLICATION.isLogin == YES) {
            [self jumpToZhuanZhang];
        }else{
            //
            //先移除本通知,原因是:点击一个功能跳到登录界面,但是在登录界面点的取消,反复操作,再点这个功能,
            //相同的通知会增加多次,登陆成功后会多次进入相应的功能
            //
            APPLICATION.jumpType = ZHUANZHANG_HOME_LOGIN;
            [[NSNotificationCenter defaultCenter] removeObserver:self name:@"LOGIN_ZZ" object:nil] ;
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpToZhuanZhang) name:@"LOGIN_ZZ" object:nil];
            LoginViewController *loginVC = [[LoginViewController alloc]init];
            [self.navigationController presentViewController:loginVC animated:YES completion:^{}];
        }
        

    }

********登录按钮点击事件********


- (IBAction)loginBtnClicked:(id)sender {
    
    if (self.userName.text.length > 0 && self.passWord.text.length > 0) {
        APPLICATION.isLogin = YES;
        
        [self dismissViewControllerAnimated:YES completion:^{
            if(APPLICATION.jumpType == ZHUANZHANG_HOME_LOGIN){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"LOGIN_ZZ" object:nil ];
            }else if (APPLICATION.jumpType == HUIKUAN_HOME_LOGIN){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"LOGIN_HK" object:nil ];
            }else if (APPLICATION.jumpType == JIEKUAN_HOME_LOGIN){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"LOGIN_JK" object:nil ];
            }
        }];
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"输入有误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
}

 

 

 

 

 

 

 

4简单的实现就完成了。

 

 

 

5开始对上面代码封装  

 

5.1增加路由类

 

//
//  RouteManager.h
//  GuideLoginDemo
//
//  Created by yfc on 17/5/11.
//  Copyright © 2017年 yfc. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface RouteManager : NSObject
+ (void)pushViewControllerWithOriginViewController:(UIViewController *)originViewController loginFlag:(NSString *)loginFlag identifier:(const NSString *)identifier otherParameters:(NSDictionary *)parameters;
@end

 

//
//  RouteManager.m
//  GuideLoginDemo
//
//  Created by yfc on 17/5/11.
//  Copyright © 2017年 yfc. All rights reserved.
//

#import "RouteManager.h"
#import "config.h"
static RouteManager *routeManager = nil;

@interface RouteManager ()
@property(nonatomic,assign)UIViewController *originViewController;
@property(nonatomic,retain)NSString *currentIdentifier;
@property(nonatomic,assign)RouteManager *manager_;

@end


@implementation RouteManager
+ (RouteManager*)sharedRouteManager{
    @synchronized(routeManager)
    {
        if(!routeManager)
        {
            routeManager = [[RouteManager alloc]init];
        }
    }
    return routeManager;
}


+ (void)pushViewControllerWithOriginViewController:(UIViewController *)originViewController loginFlag:(NSString *)loginFlag identifier:(const NSString *)identifier otherParameters:(NSDictionary *)parameters;{
    RouteManager *manager = [RouteManager sharedRouteManager];
    
    NSLog(@"manager=%@",manager);
    
    manager.manager_ = manager;
    [manager pushViewControllerWithOriginViewController:originViewController loginFlag:loginFlag   identifier:identifier otherParameters:parameters];
    
}
- (void)pushViewControllerWithOriginViewController:(UIViewController *)originViewController loginFlag:(NSString *)loginFlag identifier:(const NSString *)identifier otherParameters:(NSDictionary *)parameters;{
    self.originViewController = originViewController;
    self.currentIdentifier = (NSString *)identifier;

    if ([loginFlag isEqualToString:@"Y"]) {
        
        if (APPLICATION.isLogin == YES){
            [self pushViewController];
        }else{
            //引导登录
            [self setNotiObserverLogin];
        }
        
    }
    else{
        [self pushViewController];
    }
}
- (void)setNotiObserverLogin{
    
    APPLICATION.jumpType = ROUTE_MANAGER;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ROUTE_MANAGER" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushViewController) name:@"ROUTE_MANAGER" object:nil];
    
    //如果已经有登录页了 不要重复弹出
    UINavigationController *topNavi = (UINavigationController*)APPLICATION.window.rootViewController.presentedViewController;
    for (UIViewController *loginVC in topNavi.viewControllers) {
        if ([loginVC isKindOfClass:NSClassFromString(@"LoginViewController")]) {
            return;
        }
    }
    NSLog(@"%@",(APPLICATION.window.rootViewController.presentedViewController));
    
    LoginViewController *loginVC = [[LoginViewController alloc]init];
    [APPLICATION.window.rootViewController presentViewController:loginVC animated:YES completion:^{}];

}
- (void)pushViewController{
    {
        id viewController = [[NSClassFromString(self.currentIdentifier) alloc]init];
        if ([self.originViewController isKindOfClass:[UINavigationController class]]) {
            [(UINavigationController*)self.originViewController pushViewController:viewController animated:YES];
        }else{

            [self.originViewController.navigationController pushViewController:viewController animated:YES];
        }
        
    }
    
}
@end


5.2枚举值声明改为

 

 

typedef enum JumpType{
    ROUTE_MANAGER= 0,
}MYJumpType;


5.3三个按钮的点击事件改为

 

 

//三个按钮的点击事件
- (void)btnClicked:(UIButton *)btnn{

    if (btnn.tag == 0) {
        [RouteManager pushViewControllerWithOriginViewController:self loginFlag:@"Y" identifier:@"ZhuanZhangViewController" otherParameters:nil];
    }else if(btnn.tag == 1){
        [RouteManager pushViewControllerWithOriginViewController:self loginFlag:@"Y" identifier:@"HuiKuanViewController" otherParameters:nil];
    }else{
        [RouteManager pushViewControllerWithOriginViewController:self loginFlag:@"Y" identifier:@"JieKuanViewController" otherParameters:nil];
    }
    
}


5.4登录按钮的事件

 

 

- (IBAction)loginBtnClicked:(id)sender {
    
    if (self.userName.text.length > 0 && self.passWord.text.length > 0) {
        APPLICATION.isLogin = YES;
        
        [self dismissViewControllerAnimated:YES completion:^{
            if(APPLICATION.jumpType == ROUTE_MANAGER){
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ROUTE_MANAGER" object:nil ];
            }
        }];
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"输入有误" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
}

增加路由的代码demo:https://github.com/XiaoHeHe1/LoginGuideDemo

 

20190304备注: 成功之后的通知可改成block回调

 

 

 

 

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
UniApp是一个基于Vue.js的跨平台开发框架,可以用于开发iOS、Android和Web应用程序。实现登录注册功能可以通过以下步骤: 1. 创建登录和注册页面:在UniApp项目中创建登录和注册页面,可以使用Vue.js的模板语法编写页面布局和交互逻辑。 2. 表单验证:在登录和注册页面中,使用Vue.js的表单验证功能对用户输入进行验证,确保输入的合法性。 3. 发送请求:在登录和注册页面中,使用UniApp提供的网络请求API发送登录和注册请求到后端服务器。可以使用uni.request()方法发送POST请求,将用户输入的用户名和密码等信息发送给后端。 4. 后端处理:后端服务器接收到登录和注册请求后,进行相应的处理。对于注册请求,后端需要将用户信息保存到数据库中;对于登录请求,后端需要验证用户输入的用户名和密码是否正确。 5. 响应处理:后端服务器处理完登录和注册请求后,将结果返回给前端。前端可以通过uni.request()方法的回调函数获取到后端返回的数据,并根据返回的结果进行相应的处理,例如显示登录成功或失败的提示信息。 6. 本地存储:在登录成功后,可以使用UniApp提供的本地存储API将用户的登录状态保存到本地,以便下次打开应用时自动登录。 7. 页面跳转:根据登录和注册的结果,可以使用UniApp提供的路由功能进行页面跳转,例如登录成功后跳转到首页,注册成功后跳转登录页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值