How to call a function of web service for login? [closed]

五迷三道 提交于 2019-12-11 19:16:31

问题


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.


回答1:


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):

NSString * parameters = [NSString stringWithFormat:@"email=%@&password=%@",self.txtUserEmail.text,self.txtUserPassword.text];
[_mutableRequest setHTTPMethod:@"POST"];
NSData *requestData = [NSData dataWithBytes:[parameters UTF8String] length:[parameters length]];
[_mutableRequest setHTTPBody:requestData];

// then hit the connection using NSURLConnection



来源:https://stackoverflow.com/questions/20361049/how-to-call-a-function-of-web-service-for-login

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!