Guest User

Untitled

a guest
Feb 14th, 2016
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.10 KB | None | 0 0
  1. #import "LogIn.h"
  2. #import "BaseClass.h"
  3. #import "XMLReader.h"
  4. #import "QuestionOfTheDay.h"
  5. #import "MainView.h"
  6. #import "Config.h"
  7.  
  8. @interface LogIn () <UITextFieldDelegate,MBProgressHUDDelegate,NSXMLParserDelegate>
  9. {
  10. // NSMutableDictionary * dictUser;
  11. BOOL varSuccess;
  12. }
  13. @property (weak, nonatomic) IBOutlet UITextField *txtUserName;
  14. @property (weak, nonatomic) IBOutlet UITextField *txtPassword;
  15. @property (weak, nonatomic) IBOutlet UIImageView *imgTxt1;
  16. @property (weak, nonatomic) IBOutlet UIImageView *imgTxt2;
  17. @property (weak, nonatomic) IBOutlet UILabel *lblVersion;
  18.  
  19. @property (retain, nonatomic) NSURLConnection *connection;
  20. @property (retain, nonatomic) NSMutableData *receivedData;
  21.  
  22.  
  23. @end
  24.  
  25. @implementation LogIn
  26.  
  27. -(BOOL)textFieldShouldReturn:(UITextField *)textField
  28. {
  29. [textField resignFirstResponder];
  30. return YES;
  31. }
  32. -(void)textFieldDidBeginEditing:(UITextField *)textField
  33. {
  34. if(textField==self.txtUserName)
  35. [self.imgTxt1 setImage:[UIImage imageNamed:@"box_selected.png"]];
  36. else
  37. [self.imgTxt2 setImage:[UIImage imageNamed:@"box_selected.png"]];
  38.  
  39. }
  40. -(void)textFieldDidEndEditing:(UITextField *)textField
  41. {
  42. if(textField==self.txtUserName)
  43. [self.imgTxt1 setImage:[UIImage imageNamed:@"box_nonselected.png"]];
  44. else
  45. [self.imgTxt2 setImage:[UIImage imageNamed:@"box_nonselected.png"]];
  46.  
  47. }
  48. - (void)viewDidLoad {
  49. [super viewDidLoad];
  50.  
  51. NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
  52.  
  53. self.lblVersion.text = [NSString stringWithFormat:@"Version : %@",version];
  54.  
  55. NSDictionary * dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"loginUser"];
  56.  
  57. if(dict)
  58. [self gotoMainView:dict animated:NO];
  59.  
  60. }
  61. - (IBAction)clickHelp:(id)sender {
  62.  
  63. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://support.ebmappsllc.com/"]];
  64. }
  65. - (IBAction)clickRegister:(id)sender {
  66.  
  67. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://register.ebmappsllc.com/"]];
  68. }
  69. -(void)gotoMainView:(NSDictionary*)dict animated:(BOOL)animated
  70. {
  71. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
  72.  
  73. NSDate *fromDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"questionDate"];
  74.  
  75. NSLog(@"%@", fromDate);
  76.  
  77. if(!fromDate) // if first time
  78. {
  79. QuestionOfTheDay *yourViewController = (QuestionOfTheDay *) [storyboard instantiateViewControllerWithIdentifier:@"QuestionOfTheDay"];
  80. yourViewController.dictUser=dict;
  81. [self.navigationController pushViewController:yourViewController animated:animated];
  82. }
  83. else
  84. {
  85. NSDateComponents *difference = [[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:fromDate toDate:[NSDate date] options:0];
  86.  
  87. if([difference day]>0)
  88. {
  89. QuestionOfTheDay *yourViewController = (QuestionOfTheDay *) [storyboard instantiateViewControllerWithIdentifier:@"QuestionOfTheDay"];
  90. yourViewController.dictUser=dict;
  91. [self.navigationController pushViewController:yourViewController animated:animated];
  92. }
  93. else
  94. {
  95. MainView *yourViewController = (MainView *) [storyboard instantiateViewControllerWithIdentifier:@"MainView"];
  96. yourViewController.dictUser=dict;
  97. [self.navigationController pushViewController:yourViewController animated:animated];
  98. }
  99.  
  100. }
  101.  
  102.  
  103. }
  104. -(void)viewWillAppear:(BOOL)animated
  105. {
  106. [super viewWillAppear:animated];
  107.  
  108. self.txtUserName.text=@"";
  109. self.txtPassword.text=@"";
  110. }
  111. - (IBAction)clickLogIn:(id)sender {
  112.  
  113. [self.txtUserName resignFirstResponder];
  114. [self.txtPassword resignFirstResponder];
  115.  
  116. if(self.txtUserName.text.length==0||self.txtPassword.text.length==0)
  117. {
  118. [BaseClass showText:@"Some field missing." onView:self.view];
  119. return;
  120. }
  121.  
  122. if(![BaseClass isNetworkReachableOnView:self.view])
  123. return ;
  124.  
  125. MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
  126. [self.view addSubview:HUD];
  127. HUD.delegate = self;
  128. HUD.labelText = @"Logging In, Please wait.";
  129. [HUD showWhileExecuting:@selector(getResponse) onTarget:self withObject:nil animated:NO];
  130.  
  131. }
  132. - (void)getResponse {
  133.  
  134. // NSString *currentDeviceId = [[[UIDevice currentDevice] identifierForVendor]UUIDString];
  135. NSString *registrationID = [[NSUserDefaults standardUserDefaults]stringForKey:@"registration_id"];
  136.  
  137. NSString *login_url = [serviceBaseURL stringByAppendingFormat:@"CheckUser?userName=%@&password=%@&deviceID=%@&phoneType=%@",self.txtUserName.text,self.txtPassword.text,registrationID,@"iOS"];
  138.  
  139. NSString *loginResponse = [BaseClass requestWithURL:[NSString stringWithFormat:@"%@",login_url] onView:self.view];
  140.  
  141. // NSString * response = [BaseClass requestWithURL:[NSString stringWithFormat:@"http://prediabetes.ebmappsllc.com/Service.asmx/CheckUser?userName=%@&password=%@&deviceID=%@&phoneType=%@", self.txtUserName.text,self.txtPassword.text,currentDeviceId,@"iOS"] onView:self.view];
  142. //
  143. NSError *error = nil;
  144.  
  145. NSDictionary *dict = [XMLReader dictionaryForXMLData:[loginResponse dataUsingEncoding:NSASCIIStringEncoding] options:XMLReaderOptionsProcessNamespaces error:&error];
  146.  
  147. NSLog(@"%@",dict);
  148.  
  149. NSString * stringResponse = [[dict objectForKey:@"string"] objectForKey:@"text"];
  150.  
  151. NSString * str1 = [stringResponse substringFromIndex:[stringResponse rangeOfString:@"<Success>"].location+9];
  152.  
  153.  
  154. if([[str1 substringToIndex:[str1 rangeOfString:@"</Success>"].location] isEqualToString:@"Y"])
  155. {
  156. NSMutableDictionary *dictUser = [NSMutableDictionary dictionary];
  157.  
  158. [dictUser setObject:self.txtUserName.text forKey:@"UserName"];
  159. [dictUser setObject:self.txtPassword.text forKey:@"Password"];
  160.  
  161. [dictUser setObject:[str1 substringToIndex:[str1 rangeOfString:@"</UserId>"].location] forKey:@"UserId"];
  162.  
  163. str1 = [stringResponse substringFromIndex:[stringResponse rangeOfString:@"<UserId>"].location+8];
  164. [dictUser setObject:[str1 substringToIndex:[str1 rangeOfString:@"</UserId>"].location] forKey:@"UserId"];
  165.  
  166. str1 = [stringResponse substringFromIndex:[stringResponse rangeOfString:@"<UserGender>"].location+12];
  167. [dictUser setObject:[str1 substringToIndex:[str1 rangeOfString:@"</UserGender>"].location] forKey:@"UserGender"];
  168.  
  169. str1 = [stringResponse substringFromIndex:[stringResponse rangeOfString:@"<ActivityLevel>"].location+15];
  170. [dictUser setObject:[str1 substringToIndex:[str1 rangeOfString:@"</ActivityLevel>"].location] forKey:@"ActivityLevel"];
  171.  
  172. str1 = [stringResponse substringFromIndex:[stringResponse rangeOfString:@"<HeightInInch>"].location+14];
  173. [dictUser setObject:[str1 substringToIndex:[str1 rangeOfString:@"</HeightInInch>"].location] forKey:@"HeightInInch"];
  174.  
  175. str1 = [stringResponse substringFromIndex:[stringResponse rangeOfString:@"<WeightInLB>"].location+12];
  176. [dictUser setObject:[str1 substringToIndex:[str1 rangeOfString:@"</WeightInLB>"].location] forKey:@"WeightInLB"];
  177.  
  178. NSLog(@"user %@", dictUser);
  179.  
  180. [[NSUserDefaults standardUserDefaults] setObject:dictUser forKey:@"loginUser"];
  181. [[NSUserDefaults standardUserDefaults] synchronize];
  182.  
  183. [self registerClient];
  184.  
  185. [self gotoMainView:dictUser animated:YES];
  186.  
  187. }
  188. else
  189. {
  190. [self performSelectorOnMainThread:@selector(showAlert:) withObject:@"Login Failed." waitUntilDone:YES];
  191. }
  192.  
  193. }
  194.  
  195. -(void)registerClient{
  196.  
  197. NSError *error = nil;
  198.  
  199. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  200.  
  201. NSString *userID =[[defaults objectForKey:@"loginUser"]objectForKey:@"UserId"];
  202.  
  203. NSDictionary *userDetail = [[NSDictionary alloc]initWithObjectsAndKeys:@"iOS",@"PhoneType",[defaults objectForKey:@"registration_id"],@"DeviceID",userID,@"ID", nil];
  204.  
  205. NSMutableDictionary *registerDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:[defaults objectForKey:@"registration_id"],@"Handle",userDetail,@"UserDetails", nil];
  206.  
  207.  
  208. NSLog(@"%@",userDetail);
  209. NSLog(@"%@",registerDict);
  210.  
  211. NSData *registerJson = [NSJSONSerialization dataWithJSONObject:registerDict options:0 error:&error];
  212.  
  213. NSLog(@"%@",registerJson);
  214.  
  215. NSString * registerStr =[NSString stringWithFormat:@"%@",registerDict];
  216.  
  217. NSString *postLength = [NSString stringWithFormat:@"%lu", [registerStr length]];
  218.  
  219. // Create the request.
  220. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://ebmprediabetesnothubtestwebapp.azurewebsites.net/register/mobile/client"]];
  221.  
  222. // Specify that it will be a POST request
  223. request.HTTPMethod = @"POST";
  224.  
  225. // This is how we set header fields
  226. [request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
  227. [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
  228.  
  229. // Convert your data and set your request's HTTPBody property
  230. NSData *requestBodyData = [registerStr dataUsingEncoding:NSUTF8StringEncoding];
  231.  
  232.  
  233. request.HTTPBody = requestBodyData;
  234.  
  235. // Create url connection and fire request
  236. NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  237.  
  238. if(conn)
  239. NSLog(@"Connection is Successfull");
  240. else
  241. NSLog(@"Connection Failed");
  242.  
  243. [conn start];
  244.  
  245.  
  246. }
  247.  
  248. /*
  249. this method might be calling more than one times according to incoming data size
  250. */
  251. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
  252.  
  253. [self.receivedData appendData:data];
  254. }
  255. /*
  256. if there is an error occured, this method will be called by connection
  257. */
  258. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
  259.  
  260. NSLog(@"%@" , error);
  261. }
  262.  
  263. /*
  264. if data is successfully received, this method will be called by connection
  265. */
  266. -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
  267.  
  268.  
  269. //initialize convert the received data to string with UTF8 encoding
  270. NSString *registerResponse = [[NSString alloc] initWithData:self.receivedData
  271. encoding:NSUTF8StringEncoding];
  272. NSLog(@"Response Received %@" , registerResponse);
  273.  
  274. }
  275.  
  276.  
  277.  
  278. -(void)showAlert:(NSString*)string
  279. {
  280. [BaseClass showText:string onView:self.view];
  281.  
  282. }
  283.  
  284.  
  285.  
  286. - (void)didReceiveMemoryWarning {
  287. [super didReceiveMemoryWarning];
  288. // Dispose of any resources that can be recreated.
  289. }
  290.  
  291. /*
  292. #pragma mark - Navigation
  293.  
  294. // In a storyboard-based application, you will often want to do a little preparation before navigation
  295. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  296. // Get the new view controller using [segue destinationViewController].
  297. // Pass the selected object to the new view controller.
  298. }
  299. */
  300.  
  301. @end
Add Comment
Please, Sign In to add comment