iosjsonobjective-cparsing

iOS linking with an API


I have used the following code to link to an API and I am not getting the response. No error is shown but I am getting a null as output from the status, error and user variables. I have rechecked and the URL i have used in the MainURL variable is right too.

NSURL *url = [[NSURL alloc]initWithString:@"%@email=%@&password=%@&action=signin",MainURL, _EmailField.text,_PasswordField.text];
NSError *errors;
NSData *data = [NSData dataWithContentsOfURL:url];
NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errors];
status = json[@"status"];
error = json[@"error"];
user = json[@"user"];

if ([status isEqualToString:@"success"])
{
    [self performSegueWithIdentifier: @"LogIN" sender: self];
}
else
{
    messages=[[NSString alloc]init];
    for (int i=0; i<[error count]; i++)
    {
        messages=[messages stringByAppendingString:[error objectAtIndex:i]];
        messages=[messages stringByAppendingString:@"\n"];
    }
    
    UIAlertView *failure=[[UIAlertView alloc]initWithTitle:@"Failure" message:messages delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
    [failure show];
}

Solution

  • Try this in your code.

         NSURL *url = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"%@email=%@&password=%@&action=signin",MainURL, _EmailField.text,_PasswordField.text]];