Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new in iphone.i want to create login page.i want to check username and password with web server data.

Am using folling code. where am wrong.
-(IBAction)LoginBtnClick:(id)sender
{
if([usernameTxt.text isEqualToString:@""] || [passwordTxt.text isEqualToString:@""] )
{
[self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
}
else
{
NSString *respstring = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];

SBJsonParser *objSBJsonParser = [[SBJsonParser alloc]init];

NSDictionary *respDict = [[NSDictionary alloc]initWithDictionary:[objSBJsonParser objectWithString:respstring]];
// NSLog(@"respDict: %@",respDict);

NSDictionary *json = [[NSDictionary alloc]initWithDictionary:[respDict valueForKey:@"jsonpost" ]];
NSString *string= [NSString stringWithFormat:@"http://192.168.10.28:5000/wsserver.php&Username=%@&Password=%@",usernameTxt.text,passwordTxt.text];
NSLog(@"%@",string);
NSURL *url = [NSURL URLWithString:string];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];


//[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:[[NSString stringWithFormat:@"username=%@&password=%@",usernameTxt,passwordTxt] dataUsingEncoding:NSUTF8StringEncoding]];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"UTF-8" forHTTPHeaderField:@"charset"];
[request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];



NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", responseData);
NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"responseData: %@", str);
NSString *str1 = @"1";
if ([str isEqualToString:str1 ])
{

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Successfully" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];

[alert show];
[[self navigationController]pushViewController:snsViewController animated:YES];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Try Again" message:@"" delegate:self cancelButtonTitle:@"Try Later" otherButtonTitles:@"Call", nil];
alert.tag = 1;
[alert show];
}
}
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900