LoginViewController.m 9.8 KB

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