Login and visitor for iOS development

Login and visitor for iOS development

Discovering problems and self-revolution

In the development, there has always been such a situation: Unregistered users of the App can use some functions (visitor view). Once they need to use some core functions or obtain personalized and differentiated services, they need to log in (login customization). . The general situation is:

   >    >      >     >   
 

When I was doing development a few years ago, because the project needed to go online quickly, I didn't care about thinking (in fact, I was too culinary), and directly wrote the following code in the interface code that needs to judge the login:

BOOL isLogin;

if(self.isLogin){
   //

}
else{
   //
   //
   //
    
}
 

As time goes on and the code increases, you will find that many interfaces have repeated codes as above. This obviously violates the Do not repeat yourselfprinciple, and the logic of the jump is very annoying. So began to improve the code.

One of the first buddies' approach is to extract the login field into a constant, judge whether the value is true when needed, and then execute the corresponding logic, so that a field is omitted in each class.

if(CONSTANT.isLogin){
   //

}
else{
   //
   //
   //
    
}
 

With the increase of logic, more and more common codes are found, so the parent class is extracted, so that the logic is moved up to the parent class, and all subclasses do not need to have similar isLoginattributes to determine whether the user is logged in.

if(self.isLogin){
   //

}
else{
   //
   //
   //
    
}
 

Suddenly one day, we found that some App visitor views are very similar, and the login interface in some apps is also the same. So we found that the above code still needs to be streamlined, so we moved the above else to the parent class as well. It is to judge whether the user is logged in in the parent class, display the guest view when not logged in, and isLoginextract the field of whether the user is logged in to a special user model.

Cross the river by feeling the stones

Let's explain it with an iOS Demo. The theme interface architecture is like this:

At the beginning, when you enter, the guest interface is displayed, the color is , click the middle to +pop up the login interface, click on the login interface to +indicate the login process, and then the main interface displays the various interfaces after login, which are replaced by different colors.

The building code of the main interface is not posted here, it is very simple, mainly to explain the logic implementation of login and visitor.

  1. Parent view controller
@interface BaseViewController : UIViewController

@property (nonatomic, assign) BOOL isLogin;

@end


@implementation BaseViewController

-(void)loadView{
    
   //UserModel 
    self.isLogin = [UserModel shareInstance].isLogin;
    
   //isLogin     
   //  viewDidLoad 
   //  window rootViewController  
    self.isLogin ? [super loadView] : [self setupVisitorView];
    
}

-(void)setupVisitorView{
    
    UIView *view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    view.backgroundColor = [UIColor cyanColor];
    
    self.view = view;
    
}

@end
 
  1. Subclass view controller (four subclasses are similar, post one here)
@implementation FourViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   //Do any additional setup after loading the view.
    
    if (self.isLogin) {
        self.view.backgroundColor = [UIColor purpleColor];
    }
}

@end
 
  1. Middle view controller
@implementation CenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   //Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn setImage:[UIImage imageNamed:@"plusX_Last"] forState:UIControlStateNormal];
    
    btn.bounds = CGRectMake(0, 0, 64, 64);
    
    btn.center = self.view.center;
    
    [self.view addSubview:btn];
    
    if (!self.isLogin) {
        
        self.title = @" ";
        
        [btn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    }
    else{
    
        self.title = @" ";
        
        [btn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    
    }
    
    
    
}

-(void)login{


    [UserModel shareInstance].isLogin = YES;
    
   // rootViewController
    [self dismissViewControllerAnimated:YES completion:^{
        [UIApplication sharedApplication].keyWindow.rootViewController = [[YFTabbarController alloc]init];
    }];

}

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

@end