#import "AppDelegate.h" 中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//判断是否登陆,由登陆状态判断启动页面 //获取UserDefault
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults]; NSString *name = [userDefault objectForKey:@"name"];
//获取storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
//如果用户未登陆则把根视图控制器改变成登陆视图控制器
if (name == nil) {
NSLog(@"%@",name);
id view = [storyboard instantiateViewControllerWithIdentifier:@"LoginView"];
self.window.rootViewController = view;
}
return YES;
}
#import "ViewController.h"
#import "LapLoginViewController.h"//导入
#import "NetWorkManger.h"//导入(这是一个我自己写的第三方, 解析 url 的,可以在我博客里找一下,此处不再累赘);
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextFiled;
@property (nonatomic,strong)NSDictionary *dataSource;
@end
@implementation ViewController
- (NSDictionary *)dataSource {
if (!_dataSource) {
self.dataSource = [NSDictionary dictionary];
}
return _dataSource;
}
//storyboard 中拖出来的按钮,登陆
- (IBAction)loginAction:(id)sender {
//获取用户输入的信息
NSString *username = self.nameTextFiled.text;
NSString *password = self.passwordTextFiled.text;
NSString *str = [NSString stringWithFormat:@"http://hy.gdhstz.com/Hander/Admin_Handler.ashx?Action=Login&UserPhone=%@&FPwd=%@",username,password];
[[NetWorkManger mainNetworkManager] getDateWithURL:str success:^(NetWorkManger *net, id object) {
NSLog(@"%@",object);
} fail:^(NetWorkManger *net, NSError *error) {
}];
//对用户信息的验证
if (username.length > 10){
[[NetWorkManger mainNetworkManager] getDateWithURL:str success:^(NetWorkManger *net, id object) {
if (object == nil) {
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:@"抱歉" message:@"账号密码有误,请重新输入" delegate: self cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
[alert show];
}else {
//获取userDefault单例
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//登陆成功后把用户名和密码存储到UserDefault
[userDefaults setObject:username forKey:@"name"];
[userDefaults setObject:password forKey:@"password"]; [userDefaults synchronize];
//用模态跳转到主界面
LapLoginViewController *lapVC = [[LapLoginViewController alloc] init];
[self.navigationController pushViewController:lapVC animated:YES];
}
} fail:^(NetWorkManger *net, NSError *error) {
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:@"抱歉"
message:@"账号密码有误,请重新输入"
delegate: self
cancelButtonTitle:@"确定"
otherButtonTitles: nil, nil];
[alert show];
}];
}else{
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:@"抱歉"
message:@"账号密码有误,请重新输入"
delegate: self
cancelButtonTitle:@"确定"
otherButtonTitles: nil, nil];
[alert show];
}
}
//登陆之后跳转到该界面, 该界面的button方法用来注销 登陆的;
#import "LapLoginViewController.h"
@interface LapLoginViewController ()
@end
@implementation LapLoginViewController
- (IBAction)hahahahahahahah:(id)sender {
//获取UserDefaults单例
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//移除UserDefaults中存储的用户信息
[userDefaults removeObjectForKey:@"name"]; [userDefaults removeObjectForKey:@"password"]; [userDefaults synchronize];
//获取storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
//获取注销后要跳转的页面
id view = [storyboard instantiateViewControllerWithIdentifier:@"LoginView"];
//模态展示出登陆页面
[self presentViewController:view animated:YES completion:^{ }];
}