Objectie-C - How do I get a response from an API when sending a message?

Asked

Viewed 24 times

1

I am sending a JSON to an API. When the API receives a JSON, it responds by telling me whether the procedure was successful or not. How do I get this answer?

Shipping code:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.meuSite.com.br/api/listener.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"text/json" forHTTPHeaderField:@"Content-type"];
[request setValue:[NSString stringWithFormat:@"%d", [_json length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[_json dataUsingEncoding:NSUTF8StringEncoding]];

[[NSURLConnection alloc] initWithRequest: request delegate:self];

1 answer

1

Through the following delegates:

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    NSLog(@"did fail");
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    NSLog(@"did receive data");
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"did receive response ");
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"did finish loading");
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.