LoginViewController.m 10 KB

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