LoginViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // LoginViewController.m
  3. // Apex Mobile
  4. //
  5. // Created by Ray on 25/01/2018.
  6. // Copyright © 2018 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "LoginViewController.h"
  9. #import "RetrievePasswordViewController.h"
  10. @interface LoginViewController ()
  11. @property (strong, nonatomic) IBOutlet UILabel *verLabel;
  12. @end
  13. @implementation LoginViewController
  14. + (instancetype)viewControllerFromStoryboard {
  15. LoginViewController *loginVC = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:[LoginViewController storyboardID]];
  16. return loginVC;
  17. }
  18. - (void)viewDidLoad
  19. {
  20. [super viewDidLoad];
  21. [self.checkSavePassword setImage:[UIImage imageNamed:@"unchecked_32.png"] forState:UIControlStateNormal];
  22. [self.checkSavePassword setImage:[UIImage imageNamed:@"checked_32.png"] forState:UIControlStateSelected];
  23. self.editUser.delegate = self;
  24. self.editPassword.delegate = self;
  25. self.resize = false;
  26. self.ioffset = 0;
  27. self.title=@"Login";
  28. [self.checkSavePassword addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
  29. // [self.view addSubview:checkSavePassword];
  30. // Do any additional setup after loading the view, typically from a nib.
  31. self.mum.backgroundColor = [UIColor clearColor];
  32. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  33. NSString* shortNum =[infoDict objectForKey:@"CFBundleShortVersionString"];
  34. NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"];
  35. versionNum = [NSString stringWithFormat:@"ver:%@.A%@",shortNum,versionNum];
  36. self.verLabel.text = versionNum;
  37. }
  38. - (void)viewWillAppear:(BOOL)animated {
  39. [super viewWillAppear:animated];
  40. NSString * user = RASingleton.sharedInstance.savedUser;
  41. NSString *password = RASingleton.sharedInstance.savedPassword;
  42. if(user.length>0&&password.length>0)
  43. {
  44. self.editUser.text=user;
  45. self.editPassword.text=password;
  46. self.checkSavePassword.selected=true;
  47. }
  48. [self clearNavigationbar];
  49. [self configureNavigationBar];
  50. }
  51. - (void)viewDidAppear:(BOOL)animated {
  52. [super viewDidAppear:animated];
  53. if (RASingleton.sharedInstance.backgroundReportType == RABackgroundReportTypeNone) {
  54. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Apex & Drivers need background send location" preferredStyle:UIAlertControllerStyleAlert];
  55. UIAlertAction *rejectAction = [UIAlertAction actionWithTitle:@"Reject" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  56. RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeReject;
  57. }];
  58. UIAlertAction *alwaysAction = [UIAlertAction actionWithTitle:@"Always ask" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  59. RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeAlways;
  60. }];
  61. UIAlertAction *allowAction = [UIAlertAction actionWithTitle:@"Allow" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  62. RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeAllow;
  63. }];
  64. [alertVC addAction:rejectAction];
  65. [alertVC addAction:alwaysAction];
  66. [alertVC addAction:allowAction];
  67. [self presentViewController:alertVC animated:YES completion:nil];
  68. }
  69. }
  70. - (void)didReceiveMemoryWarning {
  71. [super didReceiveMemoryWarning];
  72. // Dispose of any resources that can be recreated.
  73. }
  74. #pragma mark - Config
  75. - (void)clearNavigationbar {
  76. self.tabBarController.navigationItem.leftBarButtonItem = nil;
  77. self.tabBarController.navigationItem.leftBarButtonItems = nil;
  78. self.tabBarController.navigationItem.titleView = nil;
  79. self.tabBarController.navigationItem.title = nil;
  80. self.tabBarController.navigationItem.rightBarButtonItem = nil;
  81. self.tabBarController.navigationItem.rightBarButtonItems = nil;
  82. }
  83. - (void)configureNavigationBar {
  84. if (self.navigationController && !self.navigationController.isNavigationBarHidden) {
  85. UIImage *logo = [[UIImage imageNamed:@"apexlogo-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  86. UIBarButtonItem *logoItem = [[UIBarButtonItem alloc] initWithImage:logo landscapeImagePhone:logo style:UIBarButtonItemStylePlain target:nil action:nil];
  87. logoItem.enabled = NO;
  88. self.navigationItem.leftBarButtonItem = logoItem;
  89. }
  90. }
  91. - (void)clear {
  92. self.editPassword.text = nil;
  93. self.editUser.text = nil;
  94. }
  95. #pragma mark - Action
  96. - (IBAction)RetrieveButtonClick:(UIButton *)sender {
  97. // [self performSegueWithIdentifier:@"RETRIEVE" sender:self];
  98. RetrievePasswordViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"RetrievePasswordViewController"];
  99. vc.preferredContentSize = CGSizeMake(330, 160);
  100. [self presentViewController:vc animated:YES completion:nil];
  101. }
  102. - (IBAction)LoginButtonClick:(UIButton *)sender {
  103. NSString*password= self.editPassword.text;
  104. NSString*user = self.editUser.text;
  105. if(user.length==0||password.length==0)
  106. {
  107. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"alert_title_error", @"Error") message:NSLocalizedString(@"alert_msg_upcanotempty", @"User&Password can not be empty!") preferredStyle:UIAlertControllerStyleAlert];
  108. UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"ok", @"Ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  109. }];
  110. [alertVC addAction:action];
  111. [self presentViewController:alertVC animated:YES completion:nil];
  112. return;
  113. }
  114. DebugLog(@"Login... user= %@ ; password= %@",self.editUser.text,self.editPassword.text);
  115. self.loginButton.enabled = false;
  116. self.mum.hidden=false;
  117. NSString *request_user = self.editUser.text.lowercaseString;
  118. NSString *request_password = self.editPassword.text;
  119. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  120. NSDictionary *loginInfo =[RADataProvider requestLogin:request_user password:request_password];
  121. int ret = [[loginInfo objectForKey:@"result"] intValue];
  122. dispatch_async(dispatch_get_main_queue(), ^{
  123. self.mum.hidden=true;
  124. self.loginButton.enabled = true;
  125. if (ret==RESULT_TRUE)
  126. {
  127. if(self.loginSuccessful)
  128. self.loginSuccessful(self.editUser.text.lowercaseString,self.editPassword.text);
  129. }
  130. else
  131. {
  132. NSString* message = nil;
  133. switch (ret) {
  134. case RESULT_NET_NOTAVAILABLE:
  135. message = NSLocalizedString(@"net_not_available", @"Network not available, please check.");
  136. break;
  137. case RESULT_NET_ERROR:
  138. message = NSLocalizedString(@"net_error", @"Network error, can not access server.");
  139. break;
  140. case RESULT_FALSE:
  141. message = NSLocalizedString(@"auth_error", @"Can not login, user or password may not correct.");
  142. break;
  143. case RESULT_VER_LOW:
  144. message = NSLocalizedString(@"ver_low", @"Current App version is too low, please update.");
  145. break;
  146. default:{
  147. message = [loginInfo objectForKey:@"err_msg"];
  148. if (message.length == 0) {
  149. message=[NSString stringWithFormat:@"Failed to login code %d",ret];
  150. }
  151. }
  152. break;
  153. }
  154. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"alert_title_error", @"Error") message:message preferredStyle:UIAlertControllerStyleAlert];
  155. UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"ok", @"Ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  156. }];
  157. [alertVC addAction:action];
  158. [self presentViewController:alertVC animated:YES completion:nil];
  159. }
  160. });
  161. });
  162. }
  163. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  164. [[self view] endEditing:YES];
  165. }
  166. -(void)checkboxClick:(UIButton *)btn
  167. {
  168. btn.selected = !btn.selected;
  169. }
  170. //- (IBAction)onRetrievePassword:(UIButton *)sender {
  171. //}
  172. #pragma mark - TextFied Delegate
  173. -(BOOL)textFieldShouldReturn:(UITextField *)textField {
  174. [textField resignFirstResponder];
  175. return YES;
  176. }
  177. -(void) textFieldDidBeginEditing:(UITextField *)textField
  178. {
  179. // CGRect textFrame = self.loginButton.frame;
  180. int loginpos = self.loginButton.frame.origin.y+self.loginButton.frame.size.height;
  181. self.ioffset = 216 -(self.view.frame.size.height-loginpos);
  182. if(self.ioffset>0)
  183. {
  184. self.resize = true;
  185. NSTimeInterval animationDuration = 0.30f;
  186. CGRect frame = self.view.frame;
  187. frame.origin.y -=self.ioffset;//view的Y轴上移
  188. frame.size.height +=self.ioffset; //View的高度增加
  189. self.view.frame = frame;
  190. [UIView beginAnimations:@"ResizeView" context:nil];
  191. [UIView setAnimationDuration:animationDuration];
  192. self.view.frame = frame;
  193. [UIView commitAnimations];//设置调整界面的动画效果
  194. }
  195. }
  196. /**
  197. 结束编辑UITextField的方法,让原来的界面还原高度
  198. */
  199. -(void) textFieldDidEndEditing:(UITextField *)textField
  200. {
  201. // if(prewTag == -1) //当编辑的View不是需要移动的View
  202. // {
  203. // return;
  204. // }
  205. // float moveY ;
  206. if(self.resize)
  207. {
  208. NSTimeInterval animationDuration = 0.30f;
  209. CGRect frame = self.view.frame;
  210. // if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
  211. // { //还原界面
  212. // moveY = prewMoveY;
  213. frame.origin.y +=self.ioffset;
  214. frame.size. height -=self.ioffset;
  215. self.view.frame = frame;
  216. // }
  217. //self.view移回原位置
  218. [UIView beginAnimations:@"ResizeView" context:nil];
  219. [UIView setAnimationDuration:animationDuration];
  220. self.view.frame = frame;
  221. [UIView commitAnimations];
  222. [textField resignFirstResponder];
  223. self.ioffset=0;
  224. }
  225. }
  226. - (void)alertTitle:(NSString *)title withMessage:(NSString *)msg {
  227. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
  228. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"ok", @"Ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  229. }];
  230. [alertVC addAction:okAction];
  231. [self presentViewController:alertVC animated:YES completion:nil];
  232. }
  233. @end