| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- //
- // 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"
- @interface LoginViewController ()
- @property (strong, nonatomic) IBOutlet UILabel *verLabel;
- @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:@"unchecked_32.png"] forState:UIControlStateNormal];
- [self.checkSavePassword setImage:[UIImage imageNamed:@"checked_32.png"] forState:UIControlStateSelected];
-
-
- self.editUser.delegate = self;
- self.editPassword.delegate = self;
- self.resize = false;
- self.ioffset = 0;
-
-
- self.title=@"Login";
-
-
- [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 stringWithFormat:@"ver:%@.A%@",shortNum,versionNum];
-
- self.verLabel.text = versionNum;
- }
- - (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=true;
-
- }
-
- [self clearNavigationbar];
- [self configureNavigationBar];
- }
- - (void)viewDidAppear:(BOOL)animated {
-
- [super viewDidAppear:animated];
-
- if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeNone) {
-
- UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Apex & Drivers need background send location" preferredStyle:UIAlertControllerStyleAlert];
-
- UIAlertAction *rejectAction = [UIAlertAction actionWithTitle:@"Reject" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeReject;
- }];
-
- UIAlertAction *alwaysAction = [UIAlertAction actionWithTitle:@"Always ask" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeAlways;
- }];
-
- UIAlertAction *allowAction = [UIAlertAction actionWithTitle:@"Allow" 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 - 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:@"apexlogo-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
- UIBarButtonItem *logoItem = [[UIBarButtonItem alloc] initWithImage:logo landscapeImagePhone:logo style:UIBarButtonItemStylePlain target:nil action:nil];
- logoItem.enabled = NO;
- self.tabBarController.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];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- self.mum.hidden=true;
- self.loginButton.enabled = true;
-
- if (ret==RESULT_TRUE)
- {
- if(self.loginSuccessful)
- self.loginSuccessful(self.editUser.text.lowercaseString,self.editPassword.text);
- }
- 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;
- }
- //- (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
|