// // SignupViewController.m // RA TradeFiling // // Created by Rui Zhang on 1/19/21. // #import "SignupViewController.h" #import "const.h" #import "RAUtils.h" #import "RADataProvider.h" @interface SignupViewController () @property(nonatomic ,strong) UITextField * firstResponderTextF;//记录将要编辑的输入框 @property (assign) int y; @property (assign) int offset; @end @implementation SignupViewController - (void)viewDidLoad { [super viewDidLoad]; // [self.navigationController.navigationBar setTintColor:UIColor.blueColor]; // BarItem颜色 // [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColor.lightTextColor}]; [self.btn_agree setImage:[UIImage imageNamed:@"checkbox_unchecked_30.png"] forState:UIControlStateNormal]; [self.btn_agree setImage:[UIImage imageNamed:@"checkbox_30.png"] forState:UIControlStateSelected]; self.btn_signup.enabled=false; self.tf_email.delegate=self; self.tf_pass0.delegate=self; self.tf_pass1.delegate=self; _offset=0; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; // Do any additional setup after loading the view. } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.y=self.view.frame.origin.y; // [IQKeyboardManager sharedManager].enable = NO; // [self.navigationController.navigationBar setHidden:false]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; // [IQKeyboardManager sharedManager].enable = YES; } - (IBAction)onAgreeClick:(id)sender { self.btn_agree.selected = !self.btn_agree.selected; if(self.btn_agree.selected) { self.btn_signup.enabled=true; } else { self.btn_signup.enabled=false; } } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if ([self.firstResponderTextF isFirstResponder]) [self.firstResponderTextF resignFirstResponder]; [[self view] endEditing:YES]; } - (IBAction)onSignUpClick:(id)sender { NSError *error = nil; NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error]; NSString *email = self.tf_email.text.lowercaseString; NSString * password = self.tf_pass0.text; NSString * password1 = self.tf_pass1.text; if(email.length<1||password.length<1) { // [RAUtils message_alert:@"EMAIL or PASSWORD can't be empty." title:@"ERROR" controller:self]; [RAUtils message_box:@"ERROR" message:@"EMAIL or PASSWORD can't be empty." completion:nil]; return; } bool pass_match = false; if([password isEqualToString:password1]) { pass_match=true; } if(!pass_match) { [RAUtils message_box:@"ERROR" message:@"PASSWORD does not match." completion:nil]; // [RAUtils message_alert:@"PASSWORD does not match." title:@"ERROR" controller:self]; return; } bool email_valid=[RAUtils validateEmail:email]; if(!email_valid) { // [RAUtils message_alert:@"EMAIL is invalid." title:@"ERROR" controller:self]; [RAUtils message_box:@"ERROR" message:@"EMAIL is invalid." completion:nil]; return; } bool pass_valid =[RAUtils checkPassword:password]; if(!pass_valid) { [RAUtils message_box:@"ERROR" message:@"PASSWORD is invalid.\r\nPassword must 8-16 characters, letters, numbers and uppercase letters are allowed." completion:nil]; // [RAUtils message_alert:@"PASSWORD is invalid.\r\nPassword must 8-16 characters, letters, numbers and uppercase letters are allowed." title:@"ERROR" controller:self]; return; } [RADataProvider Signup:email password:password completionHandler:^(NSMutableDictionary *result) { int ret=[result[@"result"] intValue]; // self.mum.hidden=true; // self.loginButton.enabled = true; if (ret==RESULT_TRUE) { [RAUtils message_box:@"Signup" message:@"Successful" completion:^{ [self.navigationController popToRootViewControllerAnimated:YES]; }]; // [RAUtils message_alert:@"Successful" title:@"Signup" controller:self action_handler:^(UIAlertAction * _Nonnull action) { // [self.navigationController popToRootViewControllerAnimated:YES];; // } completion:^{ // // // // }]; // NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; // [defaults removeObjectForKey:@"user"]; // [defaults removeObjectForKey:@"password"]; // // if(self.checkSavePassword.selected) // { // // // [defaults setValue:[AESCrypt encrypt:self.editUser.text.lowercaseString password:@"usai"] forKey:@"user"]; // [defaults setValue:[AESCrypt encrypt:self.editPassword.text password:@"usai"] forKey:@"password"]; // [defaults setBool:TRUE forKey:@"autologin"]; // } // // // [defaults synchronize]; // [RASingleton.sharedInstance login:self.editUser.text.lowercaseString password:self.editPassword.text firstName:result[@"nick_name"] companyList:result[@"company"] functions:result[@"function_panel"]]; // RASingleton.sharedInstance.fields=result[@"fields"]; // //// RASingleton.sharedInstance.user=self.editUser.text.lowercaseString; //// RASingleton.sharedInstance.password=self.editPassword.text; // [[NSNotificationCenter defaultCenter] postNotificationName:RA_NOTIFICATION_LOGIN object:nil]; // // if(!self.checkSavePassword.selected) // { // self.editUser.text=@""; // self.editPassword.text=@""; // } // // if(self.loginSuccessful) // self.loginSuccessful(); // [self dismissViewControllerAnimated:true completion:^{ // ; // }]; // [self performSegueWithIdentifier:@"LOGIN" sender:self]; } else { [[NSNotificationCenter defaultCenter] postNotificationName:RA_NOTIFICATION_LOGIN_FAILED object:nil]; // int a = RESULT_NET_NOTAVAILABLE; NSString* message = nil; switch (ret) { case RESULT_NET_NOTAVAILABLE: message = NSLocalizedString(@"net_not_available", nil); break; case RESULT_NET_ERROR: message = NSLocalizedString(@"net_error", nil); break; case RESULT_FALSE: message = NSLocalizedString(@"auth_error", nil); break; case RESULT_VER_LOW: message = NSLocalizedString(@"ver_low", nil); break; case RESULT_FAILED_WITH_MESSAGE: message = result[@"err_msg"]; break; default: message=[NSString stringWithFormat:@"Failed to login code %d",ret]; break; } UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"alert_title_error", nil) message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alertVC addAction:action]; [self presentViewController:alertVC animated:YES completion:nil]; } }]; } - (void)dealloc{ //移除键盘通知监听者 [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } #pragma maek UITextFieldDelegate - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ self.firstResponderTextF = textField;//当将要开始编辑的时候,获取当前的textField return YES; } - (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } #pragma mark : UIKeyboardWillShowNotification/UIKeyboardWillHideNotification - (void)keyboardWillShow:(NSNotification *)notification{ self.navigationController.navigationBar.translucent=NO; CGRect rect = [self.firstResponderTextF.superview convertRect:self.firstResponderTextF.frame toView:self.view];//获取相对于self.view的位置 NSDictionary *userInfo = [notification userInfo]; NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];//获取弹出键盘的fame的value值 CGRect keyboardRect = [aValue CGRectValue]; keyboardRect = [self.view convertRect:keyboardRect fromView:self.view.window];//获取键盘相对于self.view的frame ,传window和传nil是一样的 CGFloat keyboardTop = keyboardRect.origin.y; NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//获取键盘弹出动画时间值 NSTimeInterval animationDuration = [animationDurationValue doubleValue]; if (keyboardTop < CGRectGetMaxY(rect)) {//如果键盘盖住了输入框 CGFloat gap = keyboardTop - CGRectGetMaxY(rect) - 10;//计算需要网上移动的偏移量(输入框底部离键盘顶部为10的间距) _offset = _offset+gap; // __weak typeof(self)weakSelf = self; // [UIView animateWithDuration:animationDuration animations:^{ // // }]; self.view.frame = CGRectMake(self.view.frame.origin.x, _offset, self.view.frame.size.width, self.view.frame.size.height); } } - (void)keyboardWillHide:(NSNotification *)notification{ NSDictionary *userInfo = [notification userInfo]; NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//获取键盘隐藏动画时间值 NSTimeInterval animationDuration = [animationDurationValue doubleValue]; if (self.view.frame.origin.y < 0) {//如果有偏移,当影藏键盘的时候就复原 _offset = 0; self.view.frame = CGRectMake(self.view.frame.origin.x, self.y, self.view.frame.size.width, self.view.frame.size.height); // __weak typeof(self)weakSelf = self; // [UIView animateWithDuration:animationDuration animations:^{ // self.navigationController.navigationBar.translucent=NO; // // // }]; } } //#pragma mark - UITextViewDelegate //- (void)textViewDidEndEditing:(UITextView *)textView //{ // if(textView.text.length < 1){ // textView.text = @"ADDRESS"; // textView.textColor = PlaceHolderColor; // } //} //- (void)textViewDidBeginEditing:(UITextView *)textView //{ // if([textView.text isEqualToString:@"ADDRESS"]){ // textView.text=@""; // textView.textColor=[UIColor blackColor]; // } //} /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end