如何调用Web服务的loginfunction?

我在PHP中有一个Web服务API,我想在我的ios应用程序中使用这个API进行login。

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"; } 

此Web服务检查数据库查询以匹配电子邮件和密码。

现在我已经build立了与Web服务的连接,它正在返回我的响应Digitalnet Api。

在button事件中

 - (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"); } } 

我应该怎么做才能使用这个Web服务进行login,以便通过点击button从应用程序发送请求,Api使用电子邮件和密码validation请求,然后在用户通过身份validation之后,将允许移动到下一个屏幕/视图。

注意:Api包含在类include / DB_Functions.php中,它将用mysql db检查用户validation,所以请不要与Web服务混淆,我只需要知道IPHONE上的逻辑。

你说,你正在从networking服务获得回应。 这意味着它工作正常。 我认为有一些问题从url发送到Web服务的发布参数。

尝试像这样(只是一段代码,你需要相应地修改它):

 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]; 

//然后使用NSURLConnection命中连接