PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/nextdoor/LoginViewController.m

https://gitlab.com/M.Umair/nextdoor
Objective C | 282 lines | 154 code | 98 blank | 30 comment | 6 complexity | a025f4c4f86a69a7dbadbc1ea505a95a MD5 | raw file
  1. //
  2. // LoginViewController.m
  3. // nextdoor
  4. //
  5. // Created by Muhammad Usman on 15/07/2016.
  6. // Copyright (c) 2016 faiza zahid. All rights reserved.
  7. //
  8. #import "LoginViewController.h"
  9. #import <QuartzCore/QuartzCore.h>
  10. #import "TabVC.h"
  11. #import "RegisterViewController.h"
  12. #import "UserProfileVC.h"
  13. #import "ForgotPassVC.h"
  14. #import "AFNetworking.h"
  15. #import "PostViewController.h"
  16. @interface LoginViewController ()
  17. {
  18. NSDictionary *responseDict;
  19. NSString *loginSession;
  20. }
  21. @end
  22. @implementation LoginViewController
  23. //@synthesize sessid;
  24. //@synthesize session_name;
  25. //@synthesize token;
  26. - (BOOL)textFieldShouldReturn:(UITextField *)textField{
  27. [textField resignFirstResponder];
  28. return YES;
  29. }
  30. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  31. {
  32. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  33. if (self) {
  34. // Custom initialization
  35. }
  36. return self;
  37. }
  38. - (void)viewDidLoad
  39. {
  40. [super viewDidLoad];
  41. // Do any additional setup after loading the view.
  42. _btn_login.layer.cornerRadius = 5;
  43. _btn_glogin.layer.cornerRadius = 5;
  44. _btn_fblogin.layer.cornerRadius = 5;
  45. _btn_caguest.layer.cornerRadius = 5;
  46. //byDefualt Value of loginSession
  47. // PostViewController *postviewcontroller = [[PostViewController alloc]init];
  48. self.navigationController.navigationBar.tintColor=[UIColor blackColor];
  49. }
  50. //------fields show up when keybord clicks---------
  51. -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  52. [self animateTextField: textField up: YES];
  53. }
  54. - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
  55. [self animateTextField:textField up:NO];
  56. }
  57. - (void) animateTextField: (UITextField*) textField up: (BOOL) up
  58. {
  59. const int movementDistance = 20; // tweak as needed
  60. const float movementDuration = 0.3f; // tweak as needed
  61. int movement = (up ? -movementDistance : movementDistance);
  62. [UIView beginAnimations: @"anim" context: nil];
  63. [UIView setAnimationBeginsFromCurrentState: YES];
  64. [UIView setAnimationDuration: movementDuration];
  65. self.view.frame = CGRectOffset(self.view.frame, 0, movement);
  66. [UIView commitAnimations];
  67. }
  68. //- (BOOL) textFieldShouldReturn:(UITextField *)textField{
  69. // [textField resignFirstResponder];
  70. // return YES;
  71. //}
  72. //---------end of fields showup code
  73. -(void)viewWillDisappear:(BOOL)animated
  74. {
  75. [super viewWillDisappear:animated];
  76. self.navigationController.navigationBarHidden = YES;
  77. }
  78. -(void) viewWillAppear:(BOOL)animated
  79. {
  80. [super viewWillAppear:animated];
  81. self.navigationController.navigationBarHidden = NO;
  82. }
  83. - (void)didReceiveMemoryWarning
  84. {
  85. [super didReceiveMemoryWarning];
  86. // Dispose of any resources that can be recreated.
  87. }
  88. - (IBAction)onClick_btn_Continue:(id)sender {
  89. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  90. // saving an NSString
  91. loginSession = @"visiter";
  92. [prefs setObject:loginSession forKey:@"state"];
  93. TabVC *tabVC = (TabVC *) [self.storyboard instantiateViewControllerWithIdentifier:@"TabVC"];
  94. [self.navigationController pushViewController:tabVC animated:YES];
  95. }
  96. - (IBAction)onClick_btn_SignUP:(id)sender
  97. {
  98. RegisterViewController *sighUP = (RegisterViewController *) [self.storyboard instantiateViewControllerWithIdentifier:@"RegisterViewController"];
  99. [self.navigationController pushViewController:sighUP animated:YES];
  100. }
  101. - (IBAction)onClick_btn_Login:(id)sender {
  102. [self.view endEditing:YES];
  103. // UserProfileVC *userLogin= (UserProfileVC *) [self.storyboard instantiateViewControllerWithIdentifier:@"UserProfileVC"];
  104. // [self.navigationController pushViewController:userLogin animated:YES];
  105. if ( ([_txt_username.text length] != 0) && ([_txt_password.text length] != 0)){
  106. // login services code
  107. NSURL *url = [NSURL URLWithString:@"http://mantis.vu.edu.pk"];
  108. NSDictionary *paramDict = [NSDictionary dictionaryWithObjectsAndKeys:_txt_username.text,@"username",
  109. _txt_password.text,@"password",
  110. nil];
  111. AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:url];
  112. NSMutableURLRequest *request = [client requestWithMethod:@"POST"
  113. path:@"/nextdoor/note/user/login.json"
  114. parameters:paramDict];
  115. AFJSONRequestOperation *jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
  116. {
  117. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  118. // saving an NSString
  119. loginSession = @"user";
  120. [prefs setObject:loginSession forKey:@"state"];
  121. TabVC *tabVC = (TabVC *) [self.storyboard instantiateViewControllerWithIdentifier:@"TabVC"];
  122. [self.navigationController pushViewController:tabVC animated:YES];
  123. NSLog(@"Users API Response: %@", JSON);
  124. responseDict = (NSDictionary*)JSON;
  125. NSString *sessid = [NSString stringWithString:[responseDict valueForKey:@"sessid"]];
  126. NSString *session_name = [NSString stringWithString:[responseDict valueForKey:@"session_name"]];
  127. NSString *token = [NSString stringWithString:[responseDict valueForKey:@"token"]];
  128. NSUserDefaults *userdata = [NSUserDefaults standardUserDefaults];
  129. // saving an NSString
  130. [userdata setObject:sessid forKey:@"sessid"];
  131. [userdata setObject:session_name forKey:@"session_name"];
  132. [userdata setObject:token forKey:@"token"];
  133. [userdata synchronize];
  134. NSDictionary *dataArray = [NSDictionary dictionaryWithDictionary:[responseDict objectForKey:@"user"]];
  135. NSDictionary *field_mobile_no =[NSDictionary dictionaryWithDictionary:[dataArray valueForKey:@"field_mobile_no_"]];
  136. NSLog(@"post status data.....%@" , field_mobile_no );
  137. NSMutableArray *und_array = [[NSMutableArray alloc] init];
  138. for (NSDictionary *objectDict in [field_mobile_no objectForKey:@"und"])
  139. {
  140. NSLog(@"objectdictionary .... %@" , objectDict);
  141. [und_array addObject:objectDict];
  142. }
  143. NSLog(@"First Index of data array is %@",[und_array objectAtIndex:0]);
  144. NSDictionary *prinitngDict = [und_array objectAtIndex:0];
  145. NSString *mobile = [prinitngDict valueForKey:@"value"];
  146. NSLog(@"Mobile number is : %@" , mobile);
  147. NSLog(@"User Data %@" , dataArray);
  148. NSString *uid = [NSString stringWithString:[dataArray objectForKey:@"uid"]];
  149. NSString *name = [NSString stringWithString:[dataArray objectForKey:@"name"]];
  150. NSString *mail = [NSString stringWithString:[dataArray objectForKey:@"mail"]];
  151. // saving an NSString
  152. [prefs setObject:_txt_password.text forKey:@"pass"];
  153. [prefs setObject:uid forKey:@"uid"];
  154. [prefs setObject:mail forKey:@"mail"];
  155. [prefs setObject:name forKey:@"name"];
  156. [prefs setObject:mobile forKey:@"mobile_no"];
  157. [prefs synchronize];
  158. //NSLog(@"User Data %@" , uid);
  159. }
  160. failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
  161. {
  162. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops something went wrong."
  163. message:[error localizedDescription]
  164. delegate:nil
  165. cancelButtonTitle:@"Ok"
  166. otherButtonTitles:nil];
  167. [alertView show];
  168. }];
  169. [jsonOperation start];
  170. }else{
  171. UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:@"Missing required data"
  172. message:@"First enter email and password"
  173. delegate:nil
  174. cancelButtonTitle:@"OK"
  175. otherButtonTitles:nil];
  176. [alertView show];
  177. }
  178. //sessions
  179. }
  180. - (IBAction)onClick_btn_forgotPass:(id)sender {
  181. ForgotPassVC *password = (ForgotPassVC *) [self.storyboard instantiateViewControllerWithIdentifier:@"ForgotPassVC"];
  182. [self.navigationController pushViewController:password animated:YES];
  183. }
  184. @end