Ankur Kumawat Ankur Kumawat - 3 months ago 23
Objective-C Question

How to successfully login using by php web service

Firstly I have two text fields first is login and second is password and one login button. I am using a storyboard and login button connected to another view controller by push segue. This time working in my project, Put username and password in textfield and select login button and print server response in console.
I want to login successfully after move another view and login is failed don't move another view.

My php code



<?php
header('Content-type: application/json');
include('../conn.php');
if($_POST)
{
$loginid = $_POST['loginid'];
$loginpassword = $_POST['loginpassword'];
$schoolid = substr_id($loginid);
$table = tb3($schoolid);//profile
$sql=mysql_query("select * from $table where ID = '".$loginid."' AND PASSWORD = '".$loginpassword."'",$conn);

$row=mysql_fetch_assoc($sql);
if(mysql_num_rows($sql)>0)
{
echo '{"success":1}';
}
else
{
echo '{"success":0,"error_message":"UserID and/or password is invalid."}';
}
}
else
{
echo '{"success":0,"error_message":"UserID and/or password is invalid."}';
}


My viewcontroller code



- (IBAction)Login:(id)sender {
if([[self.user_id text] isEqualToString:@""] || [[self.password text] isEqualToString:@""] ) {

} else {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://sixthsenseit.com/school/project/ios/login.php"]];
//create the Method "GET" or "POST"
[request setHTTPMethod:@"POST"];
//Pass The String to server(YOU SHOULD GIVE YOUR PARAMETERS INSTEAD OF MY PARAMETERS)
NSString *userUpdate =[NSString stringWithFormat:@"loginid=%@&loginpassword=%@&",_user_id.text,_password.text, nil];

//Check The Value what we passed
NSLog(@"the data Details is =%@", userUpdate);
//Convert the String to Data
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];
//Apply the data to the body
[request setHTTPBody:data1];
//Create the response and Error
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

//This is for Response
NSLog(@"got response==%@", resSrt);

if(resSrt)
{
NSLog(@"got response");
}
else
{
NSLog(@"faield to connect");
}
}
}

Answer

This line is wrong

NSString *userUpdate =[NSString stringWithFormat:@"loginid=%@&loginpassword=%@&",_user_id.text,_password.text, nil];

you are additionally added the & in your params ,this is not in loginpassword=%@& , you need to call like loginpassword=%@ remove and send the request

use like

NSString *userUpdate =[NSString stringWithFormat:@"loginid=%@&loginpassword=%@",_user_id.text,_password.text, nil];

The problem is you are not serlize your JSON

so remove this line in your NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

and I follow your Answer

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

NSLog(@"Response code: %ld", (long)[response statusCode]);

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


            NSError *error = nil;
            NSDictionary *jsonData = [NSJSONSerialization
                                      JSONObjectWithData:urlData
                                      options:NSJSONReadingMutableContainers
                                      error:&error];

          int success = [jsonData[@"success"] integerValue];


            if(success == 1)
            {
                NSLog(@"Login SUCCESS");
                [self performSegueWithIdentifier:@"login_success" sender:self];
            } else {

                NSString *error_msg = (NSString *) jsonData[@"error_message"];
                [self alertStatus:error_msg :@"Sign in Failed!" :0];
            }

        }