IOS JSON授权

时间:2014-06-15 18:32:11

标签: ios json

我的api回答:{"回复":"成功"}

所以,如果"成功" - 将记录用户

代码:

NSString *stringURL = @"........";
    NSURL *url = [[NSURL alloc] initWithString:stringURL];
    NSData *urlData = [NSData dataWithContentsOfURL:url];

    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:urlData options:kNilOptions error:&error];

    if ([[json objectForKey:@"response"] isEqual:@"success"])
        return true;
    else
        return false;

我做错了什么?

1 个答案:

答案 0 :(得分:0)

输入 NSlog 并检查响应是什么

  

也可以使用" isEqualToString "注意" isEqual "

NSURL *myURL = [NSURL URLWithString:@"Your URL"];
NSLog(@"myURL =%@", myURL);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
    //  check the list of status codes here :http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success

     if ([(NSHTTPURLResponse *)response statusCode]==200)
     {
         NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
         NSLog(@"Response from Server in string  =%@", jsonString);
         id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
         NSLog(@"Response from Server in JSON object =%@", jsonObject);
         if ([[jsonObject objectForKey:@"response"] isEqualToString:@"success"])
            // Write code for True
         else
          // Write code for False
     }
     else
     {
         // Some Error
     }
 }];