0

I am trying to get connection from server and receive response from server but I can't understand for the response code.

NSString *post = [NSString stringWithFormat:@"Username=%@&Password=%@",@"_username",@"_password"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://<MY_URL>"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
if(conn) {
    NSLog(@"Connection Successful");
} else {
    NSLog(@"Connection could not be made");
}

this is correct or not because I am not getting a response .??

now what is code for response?????

NSURLResponse *response=nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSLog(@"%@",response);
if (response) {
  //UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
  //TableViewController *obj=[story instantiateViewControllerWithIdentifier:@"table1"];
  UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Login success" message:@"correct Username or Password" delegate:self cancelButtonTitle:@"Done" otherButtonTitles: nil];
  [alert show];
  //[self.navigationController pushViewController:obj animated:YES];
}
else
{
  UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Login Failure" message:@"Incorrect Username or Password" delegate:self cancelButtonTitle:@"Done" otherButtonTitles: nil];
  [alert show];
}
Anup Gupta
  • 1,993
  • 2
  • 25
  • 40

2 Answers2

1

You need to fetch that data:

NSError *error = nil;
NSURLResponse *response = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if(responseData)  { 
    NSDictionary *results = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments  error:&error];
    NSLog(@"res---%@", results);
}

and use dictionary for further use.

Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
Vijay Palwe
  • 109
  • 10
0

ALL the code is correct only you need to add the this in your info.plist

App Transport Security Setting

  • Allow arbitrary Loads

add red box content in your info.plist

Community
  • 1
  • 1
Anup Gupta
  • 1,993
  • 2
  • 25
  • 40