php - 如何在不收到错误响应的情况下与 ObjectiveC 中的服务器进行交互?

标签 php objective-c ios nsurlconnection nsurlrequest

这是我的第一个问题:)

我真的需要一些服务器和 PHP 方面的帮助。这是问题:

我有一个 NSMutableURLRequest 与这样的 php 文件交互:

    NSInteger userID = 4;

    NSString * logInString = [NSString stringWithFormat:@"id=%i&mode=HARD", userID];
    NSData * logInData = [logInString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [logInData length]];

    NSMutableURLRequest * logInRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://myurl.lol/login.php"]];
    [logInRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [logInRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [logInRequest setHTTPMethod:@"POST"];
    [logInRequest setHTTPBody:logInData];

    [NSURLConnection sendAsynchronousRequest:logInRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if ([data length] >0 && error == nil) {
            NSString * responseString = [NSString stringWithUTF8String:data.bytes];
            NSLog(@"%@", responseString);
            [self performSelectorOnMainThread:@selector(responseWasReceived:) withObject:responseString waitUntilDone:YES];
        }
        else if ([data length] == 0 && error == nil) {
            [self performSelectorOnMainThread:@selector(didNotReceivedResponse) withObject:nil waitUntilDone:YES];
        }
        else if (error != nil) {
            [self performSelectorOnMainThread:@selector(errorDidOccurred) withObject:nil waitUntilDone:YES];

            NSLog(@"Error = %@", error);
        }
    }];

我的 PHP 是这样的:

include("database.php");

if ($_REQUEST['mode'] == 'HARD') {
    $query = mysql_query('SELECT COUNT(*) as total FROM users WHERE id = "' . $_REQUEST['id'] . '"');

    $fetch_username = mysql_fetch_object($query);
    $usernames_coincidences = $fetch_username -> total;

    if ($usernames_coincidences == 1) {
        exit("ACCESS GRANTED");
    } else {
        exit("USER DOES NOT EXIST");
    }
}

我应该收到“ACCESS GRANTED”字符串,有时它会发生,但有时我会收到错误的响应,如“ACCESS GRANTED¿”或“ACCESS GRANTEDOL”。

有什么问题?您认为我应该在方法中使用同步请求并使用 performSelector:inBackground: 执行它吗?

最佳答案

您正在尝试使用不一定以 NULL 结尾的原始数据构造 responseString

取而代之的是:

[NSString stringWithUTF8String:data.bytes];

改用这个:

[[NSString alloc] initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];

请注意,我没有考虑您是否使用 ARC。您的原始调用产生了一个自动释放的值;我的没有;确保不会漏水。

关于php - 如何在不收到错误响应的情况下与 ObjectiveC 中的服务器进行交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13997455/

相关文章:

php - 如何在一行中获取具有相同ID但在另一列中具有不同值的数据并在php中显示到表中

objective-c - 检查初始化方法的完成 block 内 Swift 变量的值

ios - 使 AVPlayer 全屏显示且无黑框

ios - 手动将委托(delegate)和数据源添加到 xib 文件中的 UICollectionView

php - php 中日期格式的惊人结果

php - .htaccess if 语句?

php - 如何编写代码以允许用户更改密码或更改用户名?

ios - 如何在与 OpenGL 混合的 GLKView 中绘制字符串

ios - 如何通过提高音量按钮拍照并在 iOS 中显示任何弹出窗口?

ios - 如何在每个显示文本的触摸按钮后打印空格