首先登录流程,逻辑应该很多,这只是一种,希望对你有帮助
首先我们进入登录界面。
第一步我们当然是要在下面这个方法里添加这几行代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([JHUserDefaultStatus isLogin] == YES) {
//如果登录状态当然要显示home页面
[[JHControllerManager ShareManager] postNotification:ControllerManagerChangeToHomeNotification userInfo:nil];
}else if ([JHUserDefaultStatus isLogin] == NO){
//如果不是登录状态要到登录界面
[[JHControllerManager ShareManager] postNotification:ControllerManagerChangeToLaunchNotification userInfo:nil];
}
}
然后我们在看看 [JHControllerManager ShareManager] 里面的具体实现,这为一个单利
#import
extern NSString * const ControllerManagerChangeToLaunchNotification;
extern NSString * const ControllerManagerChangeToHomeNotification;
@interface JHControllerManager : NSObject
+ (instancetype)ShareManager;
- (void)postNotification:(NSString *)name userInfo:(NSDictionary * )info;
@end
#import "JHControllerManager.h"
#import "AppDelegate.h"
#import "JHRootViewController.h"
#import "JHLoginViewController.h"
NSString * const ControllerManagerChangeToLaunchNotification = @"com.i-shan.mobile.change.toLaunch";
NSString * const ControllerManagerChangeToHomeNotification = @"com.i-shan.mobile.change.toHome";
@implementation JHControllerManager
+ (instancetype)ShareManager {
static JHControllerManager *controllerManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
controllerManager = [[JHControllerManager alloc] initManager];
});
return controllerManager;
}
- (instancetype)initManager {
self = [super init];
if (self) {
// 注册通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recevieNoticatLogin:) name:ControllerManagerChangeToLaunchNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotificatHome:) name:ControllerManagerChangeToHomeNotification object:nil];
}
return self;
}
- (void)postNotification:(NSString *)name userInfo:(NSDictionary * )info {
// 发布通知
[[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
}
//如果为登录状态,则keywindow为你的主控制器
- (void)receiveNotificatHome:(NSNotification *)notification
{
if ([[notification name] isEqualToString:ControllerManagerChangeToHomeNotification]) {
JHRootViewController * rv = [[JHRootViewController alloc]init];
[UIApplication sharedApplication].keyWindow.rootViewController = rv;
}
}
//如果不为登录状态则,则到登录界面,keywindow为登录界面,或者你需要让他去的界面
-(void)recevieNoticatLogin:(NSNotification *)notification
{
if ([[notification name] isEqualToString:ControllerManagerChangeToLaunchNotification]) {
JHLoginViewController * lv = [[JHLoginViewController alloc]init];
UINavigationController *tempNav = [[UINavigationController alloc] initWithRootViewController:lv];
[UIApplication sharedApplication].keyWindow.rootViewController = tempNav;
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
再看用户状态的单利,里面的内容你可以随便添加的,我只是先写出你想实现的必须的代码
#import
@interface JHUserDefaultStatus : NSObject
+ (JHUserDefaultStatus *)sharedManager;
//是否登录
+ (BOOL)isLogin;
//保存用户信息
- (void)saveLoginInfo;
//删除用户信息
- (void)clearUserInfo;
@end
#import "JHUserDefaultStatus.h"
@implementation JHUserDefaultStatus
+ (JHUserDefaultStatus *)sharedManager {
static JHUserDefaultStatus *userDefaultStatus = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
userDefaultStatus = [[JHUserDefaultStatus alloc] init];
});
return userDefaultStatus;
}
//保存用户登录信息
- (void)saveLoginInfo {
// self.Plan_count = 0;
NSString *loginStr = @"Login";
[[NSUserDefaults standardUserDefaults] setObject:loginStr forKey:@"Login"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
//删除用户信息
- (void)clearUserInfo {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Login"];
}
//是否登录
+ (BOOL)isLogin {
NSString *loginStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"Login"];
if (loginStr == NULL) {
return NO;
}
return YES;
}
@end
然后我们怎么使用,只需要在你需要让他登录,或者退出登录状态里面,掉保存和删除用户信息,然后发布通知
比方说登录
- (void)submitButtonClick:(UIButton *)sender {
[[JHUserDefaultStatus sharedManager] saveLoginInfo];
[[JHControllerManager ShareManager] postNotification:ControllerManagerChangeToHomeNotification userInfo:nil];
}
比方说退出登录
- (void)logoutButtonClick:(UIButton *)sender {
[[JHUserDefaultStatus sharedManager] clearUserInfo];
[[JHControllerManager ShareManager] postNotification:ControllerManagerChangeToLaunchNotification userInfo:nil];
}