Advertisement
Guest User

BACKUP

a guest
Mar 11th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. - (IBAction)signinClicked:(id)sender {
  2. NSInteger success = 0;
  3. @try {
  4.  
  5. if([[self.txtUsername text] isEqualToString:@""] || [[self.txtPassword text] isEqualToString:@""] ) {
  6.  
  7. [self alertStatus:@"Please enter Email and Password" :@"Sign in Failed!" :0];
  8.  
  9. } else {
  10. NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[self.txtUsername text],[self.txtPassword text]];
  11. NSLog(@"PostData: %@",post);
  12.  
  13. NSURL *url=[NSURL URLWithString:@"https://dipinkrishna.com/jsonlogin.php"];
  14.  
  15. NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
  16.  
  17. NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
  18.  
  19. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  20. [request setURL:url];
  21. [request setHTTPMethod:@"POST"];
  22. [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
  23. [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
  24. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  25. [request setHTTPBody:postData];
  26.  
  27. //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
  28.  
  29. NSError *error = [[NSError alloc] init];
  30. NSHTTPURLResponse *response = nil;
  31. NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  32.  
  33. NSLog(@"Response code: %ld", (long)[response statusCode]);
  34.  
  35. if ([response statusCode] >= 200 && [response statusCode] < 300)
  36. {
  37. NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
  38. NSLog(@"Response ==> %@", responseData);
  39.  
  40. NSError *error = nil;
  41. NSDictionary *jsonData = [NSJSONSerialization
  42. JSONObjectWithData:urlData
  43. options:NSJSONReadingMutableContainers
  44. error:&error];
  45.  
  46. success = [jsonData[@"success"] integerValue];
  47. NSLog(@"Success: %ld",(long)success);
  48.  
  49. if(success == 1)
  50. {
  51. NSLog(@"Login SUCCESS");
  52. } else {
  53.  
  54. NSString *error_msg = (NSString *) jsonData[@"error_message"];
  55. [self alertStatus:error_msg :@"Sign in Failed!" :0];
  56. }
  57.  
  58. } else {
  59. //if (error) NSLog(@"Error: %@", error);
  60. [self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0];
  61. }
  62. }
  63. }
  64. @catch (NSException * e) {
  65. NSLog(@"Exception: %@", e);
  66. [self alertStatus:@"Sign in Failed." :@"Error!" :0];
  67. }
  68. if (success) {
  69.  
  70. [self performSegueWithIdentifier:@"conditionSegue" sender:nil];
  71.  
  72.  
  73. }
  74. }
  75.  
  76. - (void) alertStatus:(NSString *)msg :(NSString *)title :(int) tag
  77. {
  78. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
  79. message:msg
  80. delegate:self
  81. cancelButtonTitle:@"Ok"
  82. otherButtonTitles:nil, nil];
  83. alertView.tag = tag;
  84. [alertView show];
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement