php - 字符串等于不工作 iOS

标签 php ios web-services string-matching

以下是我的 PHP 代码,用于发送对 Web 服务请求的响应:

 $query = "select name,email,password from User where email='".$email."' AND password='".$password."'";
 $result = mysql_query($query);
 $num_rows = mysql_num_rows($result);


 if($num_rows==1)
 {
     sendResponse(200, json_encode("match"));
 }

 if($num_rows==0)
 {
     sendResponse(204, json_encode("not match"));
 }

以下是我的 iOS 代码:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{   
    NSString *status = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@" Returned Json data : %@",status);


    if ([status isEqualToString:@"match"])
    {
        AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

        [appDelegate OpenTabController];
    }

    //Boolean flag = [status isEqualToString:@"not match"];

    //NSLog(@"flag : %d",flag);

    if ([status isEqualToString:@"not match"])
    {
        NSLog(@"not match");


        UIAlertView *alertView = [[UIAlertView alloc]
                                      initWithTitle:@"Invalid email or password"
                                      message:@""
                                      delegate:nil
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
        [alertView show];

    }
}

[status isEqualToString:@"not match"] 不起作用。

最佳答案

首先,您要将字符串转换为 json 响应。在您的 PHP 代码中,使用关联数组生成键值对,然后在 iOS 中使用 JSON 解析器将数据转换为 JSON 格式。在转换后的字典上使用 objectForKey 方法

PHP代码

if($num_rows==1)
 {
$status = "match";
$status_array = array("status"=>"$status");
     sendResponse(200, json_encode($status_array));
 }

 if($num_rows==0)
 {
$status = "not match";
$status_array = array("status"=>"$status");
     sendResponse(204, json_encode($status_array));
 }

iOS 代码 使用下面的流程解析json并据此获取需要的值。 http://www.raywenderlich.com/5492/working-with-json-in-ios-5

关于php - 字符串等于不工作 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16955458/

相关文章:

php - 删除默认 Cake 应用程序中的默认界面 CakePHP 链接?

ios - 在 Objective-c 中聊天 UI 设计

php - 如何将 mysql select join 结果作为一维数组获取?

php - PHP Select 语句错误

php - makePartial() 返回 Mockery\Exception\BadMethodCallException : Method does not exist on this mock object

iOS - 在 WKWebView 中使用 cookie

ios - 使用 Storyboard在 swift 中创建自定义 TableViewCell

java - bluemix 上支持 http posts 的 Web 服务

c# - 为什么在从 ASMX Web 方法返回派生类对象时我的基类字段没有被序列化?

web-services - 针对多个请求的高效密码散列