LoginViewController.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. @interface LoginViewController ()
  10. @end
  11. @implementation LoginViewController
  12. - (IBAction)RetrieveButtonClick:(UIButton *)sender {
  13. [self performSegueWithIdentifier:@"RETRIEVE" sender:self];
  14. }
  15. - (IBAction)LoginButtonClick:(UIButton *)sender {
  16. NSString*password= self.editPassword.text;
  17. NSString*user = self.editUser.text;
  18. if(user.length==0||password.length==0)
  19. {
  20. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: NSLocalizedString(@"alert_title_error", nil) message:NSLocalizedString(@"alert_msg_upcanotempty", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil, nil];
  21. // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
  22. [alert show];
  23. return;
  24. }
  25. DebugLog(@"Login... user= %@ ; password= %@",self.editUser.text,self.editPassword.text);
  26. self.loginButton.enabled = false;
  27. self.mum.hidden=false;
  28. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  29. int ret=[RANetwork Authorize:self.editUser.text password:self.editPassword.text];
  30. dispatch_async(dispatch_get_main_queue(), ^{
  31. self.mum.hidden=true;
  32. self.loginButton.enabled = true;
  33. if (ret==RESULT_TRUE)
  34. {
  35. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  36. [defaults removeObjectForKey:@"user"];
  37. [defaults removeObjectForKey:@"password"];
  38. // if(self.checkSavePassword.selected)
  39. {
  40. [defaults setValue:[AESCrypt encrypt:self.editUser.text password:@"usai"] forKey:@"user"];
  41. [defaults setValue:[AESCrypt encrypt:self.editPassword.text password:@"usai"] forKey:@"password"];
  42. [defaults setBool:TRUE forKey:@"autologin"];
  43. }
  44. [defaults synchronize];
  45. if(self.loginSuccessful)
  46. self.loginSuccessful();
  47. // [self dismissViewControllerAnimated:true completion:^{
  48. // ;
  49. // }];
  50. // [self performSegueWithIdentifier:@"LOGIN" sender:self];
  51. }
  52. else
  53. {
  54. // int a = RESULT_NET_NOTAVAILABLE;
  55. NSString* message = nil;
  56. switch (ret) {
  57. case RESULT_NET_NOTAVAILABLE:
  58. message = NSLocalizedString(@"net_not_available", nil);
  59. break;
  60. case RESULT_NET_ERROR:
  61. message = NSLocalizedString(@"net_error", nil);
  62. break;
  63. case RESULT_FALSE:
  64. message = NSLocalizedString(@"auth_error", nil);
  65. break;
  66. case RESULT_VER_LOW:
  67. message = NSLocalizedString(@"ver_low", nil);
  68. break;
  69. default:
  70. message=[NSString stringWithFormat:@"Failed to login code %d",ret];
  71. break;
  72. }
  73. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert_title_error", nil) message:message delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil, nil];
  74. [alert show];
  75. }
  76. });
  77. });
  78. }
  79. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  80. [[self view] endEditing:YES];
  81. }
  82. -(void)checkboxClick:(UIButton *)btn
  83. {
  84. btn.selected = !btn.selected;
  85. }
  86. //- (IBAction)onRetrievePassword:(UIButton *)sender {
  87. //}
  88. - (void)viewDidLoad
  89. {
  90. [super viewDidLoad];
  91. [self.checkSavePassword setImage:[UIImage imageNamed:@"unchecked_32.png"] forState:UIControlStateNormal];
  92. [self.checkSavePassword setImage:[UIImage imageNamed:@"checked_32.png"] forState:UIControlStateSelected];
  93. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  94. NSString * user = [AESCrypt decrypt:[defaults stringForKey:@"user"] password:@"usai"];
  95. self.editUser.delegate = self;
  96. self.editPassword.delegate = self;
  97. self.resize = false;
  98. self.ioffset = 0;
  99. NSString * password = [AESCrypt decrypt:[defaults stringForKey:@"password"] password:@"usai"];
  100. if(user.length>0&&password.length>0)
  101. {
  102. self.editUser.text=user;
  103. self.editPassword.text=password;
  104. self.checkSavePassword.selected=true;
  105. }
  106. self.title=@"Login";
  107. [self.checkSavePassword addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];
  108. // [self.view addSubview:checkSavePassword];
  109. // Do any additional setup after loading the view, typically from a nib.
  110. }
  111. - (void)didReceiveMemoryWarning
  112. {
  113. [super didReceiveMemoryWarning];
  114. // Dispose of any resources that can be recreated.
  115. }
  116. -(BOOL)textFieldShouldReturn:(UITextField *)textField {
  117. [textField resignFirstResponder];
  118. return YES;
  119. }
  120. -(void) textFieldDidBeginEditing:(UITextField *)textField
  121. {
  122. // CGRect textFrame = self.loginButton.frame;
  123. int loginpos = self.loginButton.frame.origin.y+self.loginButton.frame.size.height;
  124. self.ioffset = 216 -(self.view.frame.size.height-loginpos);
  125. if(self.ioffset>0)
  126. {
  127. self.resize = true;
  128. NSTimeInterval animationDuration = 0.30f;
  129. CGRect frame = self.view.frame;
  130. frame.origin.y -=self.ioffset;//view的Y轴上移
  131. frame.size.height +=self.ioffset; //View的高度增加
  132. self.view.frame = frame;
  133. [UIView beginAnimations:@"ResizeView" context:nil];
  134. [UIView setAnimationDuration:animationDuration];
  135. self.view.frame = frame;
  136. [UIView commitAnimations];//设置调整界面的动画效果
  137. }
  138. }
  139. /**
  140. 结束编辑UITextField的方法,让原来的界面还原高度
  141. */
  142. -(void) textFieldDidEndEditing:(UITextField *)textField
  143. {
  144. // if(prewTag == -1) //当编辑的View不是需要移动的View
  145. // {
  146. // return;
  147. // }
  148. // float moveY ;
  149. if(self.resize)
  150. {
  151. NSTimeInterval animationDuration = 0.30f;
  152. CGRect frame = self.view.frame;
  153. // if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
  154. // { //还原界面
  155. // moveY = prewMoveY;
  156. frame.origin.y +=self.ioffset;
  157. frame.size. height -=self.ioffset;
  158. self.view.frame = frame;
  159. // }
  160. //self.view移回原位置
  161. [UIView beginAnimations:@"ResizeView" context:nil];
  162. [UIView setAnimationDuration:animationDuration];
  163. self.view.frame = frame;
  164. [UIView commitAnimations];
  165. [textField resignFirstResponder];
  166. self.ioffset=0;
  167. }
  168. }
  169. @end