LoginViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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:@"unchecked_32.png"] forState:UIControlStateNormal];
  24. [self.checkSavePassword setImage:[UIImage imageNamed:@"checked_32.png"] forState:UIControlStateSelected];
  25. self.editUser.delegate = self;
  26. self.editPassword.delegate = self;
  27. self.resize = false;
  28. self.ioffset = 0;
  29. self.title=@"Login";
  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 stringWithFormat:@"ver:%@.A%@",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. self.checkSavePassword.selected=true;
  51. }
  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:@"Warning" message:@"Apex & Drivers need background send location" preferredStyle:UIAlertControllerStyleAlert];
  59. UIAlertAction *rejectAction = [UIAlertAction actionWithTitle:@"Reject" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  60. RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeReject;
  61. }];
  62. UIAlertAction *alwaysAction = [UIAlertAction actionWithTitle:@"Always ask" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  63. RASingleton.sharedInstance.backgroundReportType = RABackgroundReportTypeAlwaysAsk;
  64. }];
  65. UIAlertAction *allowAction = [UIAlertAction actionWithTitle:@"Allow" 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. dispatch_async(dispatch_get_main_queue(), ^{
  136. self.mum.hidden=true;
  137. self.loginButton.enabled = true;
  138. if (ret==RESULT_TRUE)
  139. {
  140. if(self.loginSuccessful)
  141. self.loginSuccessful(self.editUser.text.lowercaseString,self.editPassword.text);
  142. }
  143. else
  144. {
  145. NSString* message = nil;
  146. switch (ret) {
  147. case RESULT_NET_NOTAVAILABLE:
  148. message = NSLocalizedString(@"net_not_available", @"Network not available, please check.");
  149. break;
  150. case RESULT_NET_ERROR:
  151. message = NSLocalizedString(@"net_error", @"Network error, can not access server.");
  152. break;
  153. case RESULT_FALSE:
  154. message = NSLocalizedString(@"auth_error", @"Can not login, user or password may not correct.");
  155. break;
  156. case RESULT_VER_LOW:
  157. message = NSLocalizedString(@"ver_low", @"Current App version is too low, please update.");
  158. break;
  159. default:{
  160. message = [loginInfo objectForKey:@"err_msg"];
  161. if (message.length == 0) {
  162. message=[NSString stringWithFormat:@"Failed to login code %d",ret];
  163. }
  164. }
  165. break;
  166. }
  167. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"alert_title_error", @"Error") message:message preferredStyle:UIAlertControllerStyleAlert];
  168. UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"ok", @"Ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  169. }];
  170. [alertVC addAction:action];
  171. [self presentViewController:alertVC animated:YES completion:nil];
  172. }
  173. });
  174. });
  175. }
  176. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  177. [[self view] endEditing:YES];
  178. }
  179. -(void)checkboxClick:(UIButton *)btn
  180. {
  181. btn.selected = !btn.selected;
  182. }
  183. //- (IBAction)onRetrievePassword:(UIButton *)sender {
  184. //}
  185. #pragma mark - TextFied Delegate
  186. -(BOOL)textFieldShouldReturn:(UITextField *)textField {
  187. [textField resignFirstResponder];
  188. return YES;
  189. }
  190. -(void) textFieldDidBeginEditing:(UITextField *)textField
  191. {
  192. // CGRect textFrame = self.loginButton.frame;
  193. int loginpos = self.loginButton.frame.origin.y+self.loginButton.frame.size.height;
  194. self.ioffset = 216 -(self.view.frame.size.height-loginpos);
  195. if(self.ioffset>0)
  196. {
  197. self.resize = true;
  198. NSTimeInterval animationDuration = 0.30f;
  199. CGRect frame = self.view.frame;
  200. frame.origin.y -=self.ioffset;//view的Y轴上移
  201. frame.size.height +=self.ioffset; //View的高度增加
  202. self.view.frame = frame;
  203. [UIView beginAnimations:@"ResizeView" context:nil];
  204. [UIView setAnimationDuration:animationDuration];
  205. self.view.frame = frame;
  206. [UIView commitAnimations];//设置调整界面的动画效果
  207. }
  208. }
  209. /**
  210. 结束编辑UITextField的方法,让原来的界面还原高度
  211. */
  212. -(void) textFieldDidEndEditing:(UITextField *)textField
  213. {
  214. // if(prewTag == -1) //当编辑的View不是需要移动的View
  215. // {
  216. // return;
  217. // }
  218. // float moveY ;
  219. if(self.resize)
  220. {
  221. NSTimeInterval animationDuration = 0.30f;
  222. CGRect frame = self.view.frame;
  223. // if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
  224. // { //还原界面
  225. // moveY = prewMoveY;
  226. frame.origin.y +=self.ioffset;
  227. frame.size. height -=self.ioffset;
  228. self.view.frame = frame;
  229. // }
  230. //self.view移回原位置
  231. [UIView beginAnimations:@"ResizeView" context:nil];
  232. [UIView setAnimationDuration:animationDuration];
  233. self.view.frame = frame;
  234. [UIView commitAnimations];
  235. [textField resignFirstResponder];
  236. self.ioffset=0;
  237. }
  238. }
  239. - (void)alertTitle:(NSString *)title withMessage:(NSString *)msg {
  240. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
  241. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"ok", @"Ok") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  242. }];
  243. [alertVC addAction:okAction];
  244. [self presentViewController:alertVC animated:YES completion:nil];
  245. }
  246. @end