我要做的是 LOGIN 方案。我正在使用 AFNetworking 3.0 并将用户名和密码作为参数传递,如下面的代码所示:
NSDictionary *parameters = @{@"ePharmUserUsername":self.usernameTf.text,@"ePharmUserPassword":self.passwordTf.text};
AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
mgr.requestSerializer = [AFHTTPRequestSerializer serializer];
[mgr.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"content-type"];
[mgr.requestSerializer setValue:@"utf-8" forHTTPHeaderField:@"accept-charset"];
[mgr GET:LOGIN parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"response is : %@",responseObject);
if ([[responseObject objectForKey:@"success"] intValue] == 104) {
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Internal Error"
message:[responseObject objectForKey:@"message"]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{}];
[alert addAction:okButton];
[SVProgressHUD dismiss];
[self presentViewController:alert animated:YES completion:nil];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
/********************MEMORY_MANAGEMENT_PART*********************/
okButton = nil;
alert= nil;
/********************MEMORY_MANAGEMENT_PART*********************/
}
else if ([[responseObject objectForKey:@"success"] intValue] == 106) {
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Missing Credantials"
message:[responseObject objectForKey:@"message"]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{}];
[alert addAction:okButton];
[SVProgressHUD dismiss];
[self presentViewController:alert animated:YES completion:nil];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
/********************MEMORY_MANAGEMENT_PART*********************/
okButton = nil;
alert= nil;
/********************MEMORY_MANAGEMENT_PART*********************/
}
else
{
[SVProgressHUD dismiss];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"the error is: %@",error);
[SVProgressHUD dismiss];
}];
此外,我使用 PHP 作为后端, MySQL 作为数据库服务器。 这是我的PHP代码:
<?php
header('Content-type: application/json; charset=utf-8;');
$link = mysqli_connect("localhost", "root", "");
mysqli_query($link, "SET NAMES UTF8");
$db = mysqli_select_db($link, "ePharm") or die("Cannot select Database.");
$response = array();
$response["Username"] = array();
if (!empty($_POST)) {
$Username = $_POST['ePharmUserUsername'];
$Password = $_POST['ePharmUserPassword'];
if (empty($Username) || empty($Password)) {
$response["success"] = 106;
$response["message"] = "One or both of the fields are empty.";
die(json_encode($response));
}
else
{
$query = "SELECT * FROM ePharmUser WHERE ePharmUserUsername='$Username' AND ePharmUserPassword = '$Password'";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
if (empty($row)) {
$response["success"] = 108;
$response["message"] = "Invalid Username or Password";
die(json_encode($response));
}
if (!empty($row)) {
$users = array();
$users["ePharmUserId"] = $row["ePharmUserId"];
$users["ePharmUserName"] = $row["ePharmUserName"];
$users["ePharmUserPhoneNumber"] = $row["ePharmUserPhoneNumber"];
$users["ePharmUserProfilePictureUrl"] = $row["ePharmUserProfilePictureUrl"];
$users["ePharmUserPassword"] = $row["ePharmUserPassword"];
$users["ePharmUserEmail"] = $row["ePharmUserEmail"];
$users["ePharmUserUsername"] = $row["ePharmUserUsername"];
array_push($response["Username"], $users);
$response["success"] = 100;
$response["message"] = "success";
die(json_encode($response));
}
}
}
else {
$response["success"] = 104;
$response["message"] = "The App was unable to Post Data to DB Error
104";
die(json_encode($response));
}
当我从objective-c代码(客户端)发布数据时,进程到达 PHP 中的(!empty($_POST))
,它始终为false,因此转到else语句。我已经找了一些解决方案。以下是我发现的Link1 Link2 Link3,但它们并不符合我的情况。有什么帮助吗?