I have a Web-Service API in PHP and i want to do login using this api in my ios application.
if (isset($_POST['tag']) && $_POST['tag'] != '')
{
// Get tag
$tag = $_POST['tag'];
// Include Database handler
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
// response Array
$response = array("tag" => $tag, "success" => 0, "error" => 0); // check for tag type
if ($tag == 'login')
{
// Request type is check Login
$email = $_POST['email'];
$password = $_POST['password']; // check for user
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false)
{
// user found // echo json with success = 1
$response["success"] = 1;
$response["user"]["fname"] = $user["firstname"];
$response["user"]["lname"] = $user["lastname"];
$response["user"]["email"] = $user["email"];
$response["user"]["uname"] = $user["username"];
$response["user"]["uid"] = $user["unique_id"];
$response["user"]["created_at"] = $user["created_at"];
echo json_encode($response);
}
else
{
// user not found
// echo json with error = 1
$response["error"] = 1;
$response["error_msg"] = "Incorrect email or password!";
echo json_encode($response);
}
}
else
{
echo "Digitalnet API";
}
This Web-Service checks for the database query to match the email and password.
Now I have made the connection with the web service and it is returning me the response Digitalnet Api.
In the button event
- (IBAction)btnSignIn:(id)sender {
if( ([txtUserEmail.text isEqualToString:@""]) || ([txtUserPassword.text isEqualToString:@""]))
{
[self showErrorAlert];
}
NSString *post = [[NSString alloc] initWithFormat:@"email==%@ AND password==%@",self.txtUserEmail.text,self.txtUserPassword.text];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:@"http://localhost/ColorPicker/index.php"]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:postData];
NSURLResponse *response;
NSError *error;
NSData *urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; NSLog(@"Login response: is %@",str); //getting response
else if(str != nil)
{
ColorPickerViewController *cpvc =[[ColorPickerViewController alloc] init];
[self.navigationController pushViewController:cpvc animated:YES];
}
else
{ NSLog(@"some thing went wrong");
}
}
What should I do to make login with this web-service so that the request is being sent from the application on button click, and the Api Validates the Request's with email and password and then after the user is authenticated it will allowed to move to the next screen/view.
NOTE: The Api is included with the class 'include/DB_Functions.php' which will check the user validation with mysql db so please don't be confused with the Web-Service I just need to know the logic on IPHONE.
You say, you are getting response from web- service. That means it is working fine. I think there is some problem sending post parameters from url to the web-service.
Try something like this(just a piece of code. you need to modify it accordingly):
// then hit the connection using
NSURLConnection