Reputation: 1175
I have one ViewController in that one UIImage and two UITextField's one UITableView in that i want to print the data.That data coming from api.
From the first api i want to display the UIImage and UITextField's data. From the Second Api load the data into UITableView. How can do that ?
Upto now i tried this
-(void)viewDidLoad
{
[super viewDidLoad];
NSString *urlString = [NSString stringWithFormat:@"API"];
NSString *jsonString = @"";
NSData *myJSONData =[jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:myJSONData]];
[request setHTTPBody:body];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
if(str.length > 0){
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]);
mainBannerArray =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
}
else{
NSLog(@"Error");
}
}
Here is my Connection Delegate Methods.
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
myDataQA = [[NSMutableData alloc]init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[myDataQA appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if ((NSNull *)myDataQA != [NSNull null])
{
if(myDataQA.length > 0)
{
mainBannerArray =[NSJSONSerialization JSONObjectWithData:myDataQA options:NSJSONReadingAllowFragments error:nil];
}
}
My screen like this
Upvotes: 0
Views: 78
Reputation: 916
I would suggest to use PromiseKit together with AFNetworking rather then nested API calls - the code remains clean and readable. See Chaining promises section.
Upvotes: 0
Reputation: 4699
Use AFNetworking 3.0 from Github. And call the second api in success block of first API as shown below,
NSString *urlString = [NSString stringWithFormat:@"API"]
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// this for Image and Textfield
[manager GET:urlString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
// Load image and text field
// And then call sencod url
AFHTTPSessionManager *manager2 = [AFHTTPSessionManager manager];
[manager2 GET:link2 parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
// reload the tableview
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
Upvotes: 1