php - Problems converting Python http request code to Objective-C

one text

Recently I am converting my Python project to Objective-C. And I faced a problem when rewriting http request method in Objective-C. Here is the original Python implementation works perfectly:

data = {"username": username,"password": password}
status = requests.post("http://example.net/process.php",data=datas)
print(status.text)
if(status.text == "0"):
     //  Login Successfully
else:
     //  Login Failed

I attempt to make it works in Objective-C but kResponseData has no value. Here is my Objective-C code:

NSURL *address = [[NSURL alloc] initWithString: @"http://example.net/process.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: address];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-type"];
NSString *postString = [[NSString alloc] initWithFormat: @"{\"username\":\"%@\",\"password\":\"%@\"}", self.username, self.password];
NSData *postData = [postString dataUsingEncoding: NSUTF8StringEncoding];
request.HTTPMethod = @"POST";
request.HTTPBody = postData;
NSURLResponse *kResponse = nil;
NSError *error = nil;
NSData *kResponseData = [NSURLConnection sendSynchronousRequest: request returningResponse: &kResponse error: &error];
NSLog(@"%@\n", [[NSString alloc] initWithData: kResponseData encoding: NSUTF8StringEncoding], kResponseData);
if([[[NSString alloc] initWithData: kResponseData encoding: NSUTF8StringEncoding] isEqualToString: @"0"]){
//  Login Successfully
}
else{
//  Login Failed
}

And here is a part of server php:

$username = $_POST['username'];
$password = $_POST['password'];
$password = md5($password);
$sql = "SELECT * FROM USERS WHERE username = '$username'";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result) == 0){
    //  User not found
    echo '2';
    exit();
}
while($row = mysqli_fetch_assoc($result)){
    if($row['password'] == $password){
    //  Found user, password correct
    echo '0';
    exit();
}else{
    //  Incorrect password
    echo '1';
    exit();
}
}

So how can I get the login statement value in Objective-C like status.text in Python? Thanks for answering!

My efforts using NSURLSession

Currently my code is here. Some are from another post of Stack OverFlow

NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration: configuration delegate: self delegateQueue:nil];
NSURL *url = [NSURL URLWithString: @"http://example.net/process.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"text/html" forHTTPHeaderField:@"Content-type"];
//  Also tries "application/json" but same issue
[request setHTTPMethod:@"POST"];
NSString *postString = [[NSString alloc] initWithFormat: @"{\"username\":\"%@\",\"password\":\"%@\"}", username, password];
NSData *postData = [postString dataUsingEncoding: NSUTF8StringEncoding];
[request setHTTPBody: postData];
    
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *kResponse, NSError *error) {
        NSLog(@"%@", data);
        NSLog(@"%@", kResponse);
        NSLog(@"%@", error);
    }];

[postDataTask resume];

I still got empty data. kResponse and error also printed as follows:

2022-08-27 18:56:17.415 test[3699:46726] {length = 0, bytes = 0x}
2022-08-27 18:56:17.415 test[3699:46726] <NSHTTPURLResponse: 0x7ffaa5d002e0> { URL: http://example.net/process.php } { Status Code: 200, Headers {
    "Cache-Control" =     (
        "no-store, no-cache, must-revalidate"
    );
    Connection =     (
        "keep-alive"
    );
    "Content-Encoding" =     (
        gzip
    );
    "Content-Type" =     (
        "text/html; charset=UTF-8"
    );
    Date =     (
        "Sat, 27 Aug 2022 10:56:17 GMT"
    );
    Expires =     (
        "Thu, 19 Nov 1981 08:52:00 GMT"
    );
    "Keep-Alive" =     (
        "timeout=4"
    );
    Pragma =     (
        "no-cache"
    );
    "Proxy-Connection" =     (
        "keep-alive"
    );
    Server =     (
        nginx
    );
    "Strict-Transport-Security" =     (
        "max-age=31536000"
    );
    "Transfer-Encoding" =     (
        Identity
    );
    Vary =     (
        "Accept-Encoding"
    );
} }
2022-08-27 18:56:17.415 test[3699:46726] (null)

I also tried to initialize both NSDictionary and NSString for params and then go postData:

//  String method
NSString *postString = [[NSString alloc] initWithFormat: @"{\"username\":\"%@\",\"password\":\"%@\"}", self.username, self.password];
NSData *postData = [postString dataUsingEncoding: NSUTF8StringEncoding];
[request setHTTPBody: postData];
//  Dictionary method
 NSDictionary *postDictionary = [[NSDictionary alloc] initWithObjectsAndKeys: self.username ,@"username",self.password,@"password", nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject: postDictionary options:0 error:nil];
[request setHTTPBody: postData];

Both two methods got {length = 0, bytes = 0x}. Is my problem setting up params wrongly or making not appropriate request?

Source