user login using POST method in objective C

Hello everyone..I'm trying to send username and password to server side using POST method,but I don't know how to do that. Can anybody suggest me how to do it?

Here is my code..


-(void) alertStatus:(NSString*)msg : (NSString*)title

{

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

[alertView show];

}


-(IBAction) loginAction:(UIButton*)sender

{

NSString*username = _userNameField.text;

NSString*password = _passwordField.text;

NSString*post = [NSString stringWithFormat:@"Username=%@&Password=%@" ,username,password];

NSData*postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString*postLength = [NSString stringWithFormat:@"%d" ,[postData length]];

NSMutableURLRequest*request = [[NSMutableURLRequest alloc]init];

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://172.31.144.227:8080/rest/login/post"]]];

[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];

[request setHTTPBody:postData];

NSError*error = [[NSError alloc] init];

NSHTTPURLResponse*response = nil;

NSData*urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


if ([response statusCode] >=200 && [response statusCode] <300)

{

NSData*responseData = [[NSData alloc]initWithData:responseData options: NSJSONReadingMutableContainers error:nil];

if([jsonObject objectForKey:@"error"])

{

[self alertStatus:@" " :@" "];

}else{

[self alertStatus:@" " :@" "];

}

}else{

if (error) NSLog(@"Error: %@", error); //Here showing erroe after compilation[Thread1: signal SIGKILL] and [Thread 1:EXC_BAD_ACCESS(code =1,address=0xd)

[self alertStatus:@"Connection Failed!" :@"Login Failed!"];

}

}


After running this i received these following errors:-

-> 0xee8f25 <+165>: calll 0x1092168 ; symbol stub for: getpid [Thread 1:EXC_BREAKPOINT(code=EXC_I386_BPT,subcode=0x0)

I moved your thread to CoreOS > Networking because this is more about networking than it is about Objective-C.

I tried to look through your code but it’s very difficult of the formatting. When you post code, it’s helpful if you can format it as code using the

<>
icon in the editor.

From what I can tell:

  • You’re doing your networking synchronously on the main thread, which is not going to end well. QA1693 Synchronous Networking On The Main Thread has the details.

  • You’re not testing the result of

    +sendSynchronousRequest:
    , which is an absolute requirement before looking at the ‘out’ parameters (
    response
    and
    error
    ).
  • The code you posted has one reference to

    jsonObject
    without any explanation of where that comes from.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi eskimo,

Thank you for your time and suggestion.I've also tried to run this part of code and finally I wrote the correct code.But though I'm new to Xcode, it'll be a great help if you look into the following code and suggest me if any further modification is required or not. Here I'm trying to fetch the input data from the two text fields in my app and try to send it to server side using POST method.

Here is my updated code:-



- (IBAction)loginAction:(UIButton *)sender

{

NSMutableDictionary *post = [[NSMutableDictionary alloc]init];

[post setValue:self.userNameField.text forKey:@"username"];

[post setValue:self.passwordField.text forKey:@"password"];

NSArray* notifications = [NSArray arrayWithObjects:post, nil];

NSError *writeError = nil;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:notifications options:kNilOptions error:&writeError];

NSString *postLength = [NSString stringWithFormat:@"%d",[jsonData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];

[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://your/url]]];

[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length" ];

[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[request setHTTPBody:jsonData];

NSLog(@"JSON Summary: %@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

[theConnection start];

NSError *error = [[NSError alloc] init];

NSHTTPURLResponse *response = nil;

NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSLog(@"Response Error= %@", response);

if ([response statusCode] >=200 && [response statusCode] <300)

{

NSData *responseData = [[NSData alloc]initWithData:urlData];

NSMutableDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];

NSLog(@"Random Output= %@", jsonObject);

[self performSegueWithIdentifier:@"DASHBOARDSEGUE" sender:sender];

}else {

[self alertStatus:@"Connection Failed" :@"Login Failed!"];

}

}

You seem to be starting two NSURLConnections here, one async and one sync. That makes no sense.

Taking a step back, I think you need to read up on how to do networking on iOS. You can’t do synchronous networking on the main thread, per QA1693 in my previous post, so your

-loginAction:
, which is running on the main thread, won’t be able to complete the login before returning. Moreover, as a thought experiment, imagine that the network is running really slowly (which happens a lot on iOS, given the mobile nature of the devices) and your POST request takes 45 seconds to complete. What is your app doing during those 45 seconds?

Ideally you’d let the user continue using your app while the login is running. However, if the user absolutely can’t make any forward progress before the login is complete, you need some sort of UI to show to the user tell them what’s going on, preferably with a Cancel button so that they can stop it if necessary.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Pro send file server

user login using POST method in objective C
 
 
Q