// // LoginViewController.m // Apex Mobile // // Created by Ray on 25/01/2018. // Copyright © 2018 United Software Applications, Inc. All rights reserved. // #import "LoginViewController.h" #import "RetrievePasswordViewController.h" #import @interface LoginViewController () @property (strong, nonatomic) IBOutlet UILabel *verLabel; @property (nonatomic,strong) CLLocationManager *locationManager; @end @implementation LoginViewController + (instancetype)viewControllerFromStoryboard { LoginViewController *loginVC = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:[LoginViewController storyboardID]]; return loginVC; } - (void)viewDidLoad { [super viewDidLoad]; [self.checkSavePassword setImage:[UIImage imageNamed:@"uncheck"] forState:UIControlStateNormal]; [self.checkSavePassword setImage:[UIImage imageNamed:@"check"] forState:UIControlStateSelected]; self.editUser.delegate = self; self.editPassword.delegate = self; self.resize = false; self.ioffset = 0; self.title=NSLocalizedString(@"Login", nil); [self.checkSavePassword addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside]; // [self.view addSubview:checkSavePassword]; // Do any additional setup after loading the view, typically from a nib. self.mum.backgroundColor = [UIColor clearColor]; NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary]; NSString* shortNum =[infoDict objectForKey:@"CFBundleShortVersionString"]; NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"]; versionNum = [NSString localizedStringWithFormat:NSLocalizedString(@"ver:%@.A%@", nil),shortNum,versionNum]; self.verLabel.text = versionNum; // 请求定位授权 [self requestLocationAuthorization]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; NSString * user = RASingleton.sharedInstance.savedUser; NSString *password = RASingleton.sharedInstance.savedPassword; if(user.length>0&&password.length>0) { self.editUser.text=user; self.editPassword.text=password; } self.checkSavePassword.selected = RASingleton.sharedInstance.savePassword; [self clearNavigationbar]; [self configureNavigationBar]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeNone) { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Warning", nil) message:[NSString localizedStringWithFormat:NSLocalizedString(@"Do you want to response %@ location request?", nil),NSLocalizedStringFromTable(@"CFBundleDisplayName", @"InfoPlist", nil)] preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *rejectAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Reject", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeReject; }]; UIAlertAction *alwaysAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Always ask", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeAlwaysAsk; }]; UIAlertAction *allowAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Allow", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeAllow; }]; [alertVC addAction:rejectAction]; [alertVC addAction:alwaysAction]; [alertVC addAction:allowAction]; [self presentViewController:alertVC animated:YES completion:nil]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Location - (void)requestLocationAuthorization { if (self.locationManager) { return; } self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.allowsBackgroundLocationUpdates = YES; [self.locationManager requestAlwaysAuthorization]; } #pragma mark - Config - (void)clearNavigationbar { self.tabBarController.navigationItem.leftBarButtonItem = nil; self.tabBarController.navigationItem.leftBarButtonItems = nil; self.tabBarController.navigationItem.titleView = nil; self.tabBarController.navigationItem.title = nil; self.tabBarController.navigationItem.rightBarButtonItem = nil; self.tabBarController.navigationItem.rightBarButtonItems = nil; } - (void)configureNavigationBar { if (self.navigationController && !self.navigationController.isNavigationBarHidden) { UIImage *logo = [[UIImage imageNamed:@"apex_logo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIBarButtonItem *logoItem = [[UIBarButtonItem alloc] initWithImage:logo landscapeImagePhone:logo style:UIBarButtonItemStylePlain target:nil action:nil]; // logoItem.enabled = NO; self.navigationItem.leftBarButtonItem = logoItem; } } - (void)clear { self.editPassword.text = nil; self.editUser.text = nil; } #pragma mark - Action - (IBAction)RetrieveButtonClick:(UIButton *)sender { // [self performSegueWithIdentifier:@"RETRIEVE" sender:self]; RetrievePasswordViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"RetrievePasswordViewController"]; vc.preferredContentSize = CGSizeMake(330, 160); [self presentViewController:vc animated:YES completion:nil]; } - (IBAction)LoginButtonClick:(UIButton *)sender { NSString*password= self.editPassword.text; NSString*user = self.editUser.text; if(user.length==0||password.length==0) { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"alert_title_error", @"Error") message:NSLocalizedString(@"alert_msg_upcanotempty", @"User&Password can not be empty!") preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"ok", @"Ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alertVC addAction:action]; [self presentViewController:alertVC animated:YES completion:nil]; return; } DebugLog(@"Login... user= %@ ; password= %@",self.editUser.text,self.editPassword.text); self.loginButton.enabled = false; self.mum.hidden=false; NSString *request_user = self.editUser.text.lowercaseString; NSString *request_password = self.editPassword.text; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSDictionary *loginInfo =[RADataProvider requestLogin:request_user password:request_password]; int ret = [[loginInfo objectForKey:@"result"] intValue]; NSLog(@"login return %d",ret); dispatch_async(dispatch_get_main_queue(), ^{ self.mum.hidden=true; self.loginButton.enabled = true; if (ret==RESULT_TRUE) { NSString *firstName = [loginInfo objectForKey:@"firstName"]; NSString *driver = [loginInfo objectForKey:@"driver"]; if(self.loginSuccessful) self.loginSuccessful(driver,request_password, firstName); } else { NSString* message = nil; switch (ret) { case RESULT_NET_NOTAVAILABLE: message = NSLocalizedString(@"net_not_available", @"Network not available, please check."); break; case RESULT_NET_ERROR: message = NSLocalizedString(@"net_error", @"Network error, can not access server."); break; case RESULT_FALSE: message = NSLocalizedString(@"auth_error", @"Can not login, user or password may not correct."); break; case RESULT_VER_LOW: message = NSLocalizedString(@"ver_low", @"Current App version is too low, please update."); break; default:{ message = [loginInfo objectForKey:@"err_msg"]; if (message.length == 0) { message=[NSString stringWithFormat:@"Failed to login code %d",ret]; } } break; } UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"alert_title_error", @"Error") message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"ok", @"Ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alertVC addAction:action]; [self presentViewController:alertVC animated:YES completion:nil]; } }); }); } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [[self view] endEditing:YES]; } -(void)checkboxClick:(UIButton *)btn { btn.selected = !btn.selected; RASingleton.sharedInstance.savePassword = btn.selected; } //- (IBAction)onRetrievePassword:(UIButton *)sender { //} #pragma mark - TextFied Delegate -(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } -(void) textFieldDidBeginEditing:(UITextField *)textField { // CGRect textFrame = self.loginButton.frame; int loginpos = self.loginButton.frame.origin.y+self.loginButton.frame.size.height; self.ioffset = 216 -(self.view.frame.size.height-loginpos); if(self.ioffset>0) { self.resize = true; NSTimeInterval animationDuration = 0.30f; CGRect frame = self.view.frame; frame.origin.y -=self.ioffset;//view的Y轴上移 frame.size.height +=self.ioffset; //View的高度增加 self.view.frame = frame; [UIView beginAnimations:@"ResizeView" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations];//设置调整界面的动画效果 } } /** 结束编辑UITextField的方法,让原来的界面还原高度 */ -(void) textFieldDidEndEditing:(UITextField *)textField { // if(prewTag == -1) //当编辑的View不是需要移动的View // { // return; // } // float moveY ; if(self.resize) { NSTimeInterval animationDuration = 0.30f; CGRect frame = self.view.frame; // if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动 // { //还原界面 // moveY = prewMoveY; frame.origin.y +=self.ioffset; frame.size. height -=self.ioffset; self.view.frame = frame; // } //self.view移回原位置 [UIView beginAnimations:@"ResizeView" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations]; [textField resignFirstResponder]; self.ioffset=0; } } - (void)alertTitle:(NSString *)title withMessage:(NSString *)msg { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"ok", @"Ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alertVC addAction:okAction]; [self presentViewController:alertVC animated:YES completion:nil]; } @end