LoginViewController.m 12 KB

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