title: Hitting multiple API in iOS
body: <p>I am new to iOS development, and I am facing a lot of trouble in calling API. I have created a page for a chatting app where I need to display top users of that particular app in a collection view cell through pagination. I also need to show another array of images in a hexagon above that collection view cell.</p>
  
  <p>Thereby it is required for me to use two API in the same page. I have already used first URL for the collection view cell and its working fine. But am facing trouble with the second one. I have already applied the second URL but whenever am running the page, it crashes. Here is my code below:</p>
  
  <pre><code>@interface TattooSinglesScreen ()
  
  @property(nonatomic,strong) NSURLConnection *connection;
  @property NSMutableData *responseData;
  @property NSMutableDictionary *responsedic;
  @property NSMutableArray *arrname,*arrage,*arrcity,*arrdistance;
  
  @end
  
  @implementation TattooSinglesScreen{
  
  
      NSMutableData *_receivedData;
  }
  
  - (void)viewDidLoad {
  
      [super viewDidLoad];
  
      [self callApi:@"http://tattoosingles.net/webservice/tattoosingles.php" param:@"id=578&amp;call=0"];
      [self callApi:@"http://tattoosingles.net/webservice/eye.php" param:@"id=578"];
  
  
  
  
     // [self callApi:@"http://www.playmart.in/Api/GetMenu" param:@""];
      // [self callApi:@"http://tattoosingles.net/webservice/tattoosingles.php" param:@"id=578&amp;call=0"];
  
     // [self createButton];
          _arrname=[[NSMutableArray alloc]init];
  
       //   _arrTopListImages=[[NSMutableArray alloc]init];
  
          _backview.hidden=true;
  
          // Do any additional setup after loading the view, typically from a nib.
  
        scrollView.delegate = self;
          scrollView.scrollEnabled = YES;
      scrollView.pagingEnabled=YES;
  
  
     // _arrTopListImages=[[NSMutableArray alloc]initWithObjects:@"docpanic.png",@"makani.png",nil];
  
    //  NSLog(@"array top Images %lu",(unsigned long)_arrTopListImages.count);
      int i;
  
         [scrollView setAlwaysBounceVertical:NO];
      for (i=0; i&lt;[_arrTopListImages count]; i++)
      {
  
          _TopList_ImageView=[[UIImageView alloc]initWithFrame:CGRectMake(((_TopList_ImageView.frame.size.width)*i)+10, 5,_TopList_ImageView.frame.size.width, _TopList_ImageView.frame.size.height)];
  //        _TopList_ImageView.transform = CGAffineTransformMakeRotation(1.570796325);
  
          _TopList_ImageView.image=[UIImage imageNamed:[_arrTopListImages objectAtIndex:i]];
  
  
  
           _TopList_ImageView.layer.mask = [self ChangeShape:_TopList_ImageView];
  
          _TopList_ImageView.backgroundColor=[UIColor redColor];
  //        _TopList_ImageView.layer.borderWidth=4;
  //        _TopList_ImageView.layer.borderColor=[[UIColor blackColor] CGColor];
  //        _TopList_ImageView.layer.masksToBounds=YES;
  
          [scrollView addSubview:_TopList_ImageView];
      }
      scrollView.contentSize=CGSizeMake(_TopList_ImageView.frame.size.width*i+20, scrollView.frame.size.height);
      NSURL *url = [NSURL URLWithString:@"http://tattoosingles.net/webservice/tattoosingles.php"];
  
  
  
      NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:
                                     url];
  
  
  
      NSString *postData = [[NSString alloc] initWithFormat:@"id=578&amp;call=0"];
  
  
      [request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
  
      [request setHTTPMethod:@"POST"];
      [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
      [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
  
      _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  
  
      _arrNum=[[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",nil];
  
     // _arrImages= [[NSArray alloc]initWithObjects:@", nil]
    //  NSLog(@"array num %lu",(unsigned long)_arrNum.count);
  
      UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
  
      [singleTap setNumberOfTapsRequired:1];
  
      [self.view addGestureRecognizer:singleTap];
  
  }
  
  -(void)callApi : (NSString *)url param:(NSString *)parameter{
  
  NSMutableURLRequest *request =
  [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
  
  [request setHTTPMethod:@"POST"];
  
  NSString *postString = parameter;
  
  [request setValue:[NSString
                     stringWithFormat:@"%lu", (unsigned long)[postString length]]
  forHTTPHeaderField:@"Content-length"];
  
  [request setHTTPBody:[postString
                        dataUsingEncoding:NSUTF8StringEncoding]];
  NSURLConnection *con= [[NSURLConnection alloc]initWithRequest:request delegate:self];
  
  
  }
  
  
  - (void) handleSingleTap: (id) sender
  
  {
  
      [_backview setHidden:YES];
  
  }
  
  - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
      return [[_responsedic valueForKey:@"name" ] count];
  
  }
  
  
  -(CAShapeLayer*)ChangeShape:(UIView*)view
  {
      UIView *v = [[UIView alloc] initWithFrame:CGRectMake(80, 80, 80, 80)];
      v.backgroundColor = [UIColor purpleColor];
  
  
      CGRect rect = v.frame;
  
      CAShapeLayer *hexagonMask = [CAShapeLayer layer];
      CAShapeLayer *hexagonBorder = [CAShapeLayer layer];
      hexagonBorder.frame = v.layer.bounds;
      UIBezierPath *hexagonPath = [UIBezierPath bezierPath];
      CGFloat sideWidth = 2 * ( 0.5 * rect.size.width / 2 );
      CGFloat lcolumn = ( rect.size.width - sideWidth ) / 2;
      CGFloat rcolumn = rect.size.width - lcolumn;
      CGFloat height = 0.866025 * rect.size.height;
      CGFloat y = (rect.size.height - height) / 2;
      CGFloat by = rect.size.height - y;
      CGFloat midy = rect.size.height / 2;
      CGFloat rightmost = rect.size.width;
      [hexagonPath moveToPoint:CGPointMake(lcolumn, y)];
      [hexagonPath addLineToPoint:CGPointMake(rcolumn, y)];
      [hexagonPath addLineToPoint:CGPointMake(rightmost, midy)];
      [hexagonPath addLineToPoint:CGPointMake(rcolumn, by)];
      [hexagonPath addLineToPoint:CGPointMake(lcolumn, by)];
      [hexagonPath addLineToPoint:CGPointMake(0, midy)];
      [hexagonPath addLineToPoint:CGPointMake(lcolumn, y)];
  
      hexagonMask.path = hexagonPath.CGPath;
      hexagonBorder.path = hexagonPath.CGPath;
      hexagonBorder.fillColor = [UIColor clearColor].CGColor;
      hexagonBorder.strokeColor = [UIColor blackColor].CGColor;
      hexagonBorder.lineWidth = 5;
      v.layer.mask = hexagonMask;
      [v.layer addSublayer:hexagonBorder];
  
      //    hexagonMask.path = path.CGPath;
      UIGraphicsEndImageContext();
      return hexagonMask;
  }
  
  
  - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  
      static NSString *identifier = @"Cell";
  
  
       TattooSinglesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
  
      cell.layer.shouldRasterize = YES;
      cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
      cell.layer.cornerRadius = 5;
      cell.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:cell.layer.cornerRadius] CGPath];
  
      UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(6, 12, 50, 50)];
      imageview.image=[UIImage imageNamed:@"hex64.png"];
      [cell.contentView addSubview:imageview];
  
      UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(20, 22, 23, 30)];
      // label.text=@"1";
      // label.backgroundColor=[UIColor redColor];
      for (int i=0; i&lt;_arrNum.count;i++ )
      {
          //label=[[UILabel alloc]init];
          label.text=[NSString stringWithFormat:@"%@",[_arrNum objectAtIndex:indexPath.item]];
  
        //  NSLog(@"LBL TEXT %@",label.text);
  
      }
      label.textAlignment = NSTextAlignmentCenter;
      label.textColor=[UIColor whiteColor];
      label.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
      [cell.contentView addSubview:label];
  
  
      //    Code for showing text in a label in Collection cell view
  
  
      cell.lblName.text=[[[[[[_responsedic valueForKey:@"full_name"]objectAtIndex:indexPath.row]stringByAppendingString:@" "]stringByAppendingString:@"("]stringByAppendingString:[[_responsedic valueForKey:@"age"]objectAtIndex:indexPath.row]]stringByAppendingString:@")"];
  
      cell.lblcity.text=[[[[[[_responsedic valueForKey:@"city"]objectAtIndex:indexPath.row]stringByAppendingString:@"("]stringByAppendingString:[NSString stringWithFormat:@"%.2f",[[[_responsedic valueForKey:@"distance"]objectAtIndex:indexPath.row]floatValue]]]stringByAppendingString:@"km"]stringByAppendingString:@")"];
  
  
      //[NSString stringWithFormat:@"%00.02d", intValue]
  //    cell.lbldistance.text=[NSString stringWithFormat:@"%d.89",[[[_responsedic valueForKey:@"distance"]objectAtIndex:indexPath.row]intValue]];
  
          NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat: @"http://tattoosingles.net/uploads/%@",[_arrname objectAtIndex:indexPath.row]]];
      [cell.profileimage sd_setImageWithURL:url ];
                          // placeholderImage:[UIImage sd_animatedGIFNamed:@"indicator"]];
  
  
      return cell;
  }
  
  
  -(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  {
      CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
      CGFloat minSpace = 12; //Space that you want
      return  CGSizeMake((self.view.frame.size.width - minSpace) / 2-3 ,216 ); //Cell height is your collectionViewCell height.
  
  
  
  }
  
  - (void)didReceiveMemoryWarning {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  
  - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
  {
      NSLog(@"Did Receive Response %@", response);
      _responseData = [[NSMutableData alloc]init];
      _receivedData=[[NSMutableData alloc] init];
  
  }
  - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
  {
      //NSLog(@"Did Receive Data %@", data);
      //NSLog(@"%@",data);
      [_responseData appendData:data];
  
      [_receivedData appendData:data];
  
   //   NSLog(@"priya%@",_responseData);
  
      //    NSError * error = nil;
      //    NSArray  *responseArray = [ NSJSONSerialization JSONObjectWithData :_responseData options : 0 error :&amp;error];
      //    NSLog(@"%@",responseArray);
  }
  - (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
  {
  
      if (_receivedData == nil) {
          //  NSLog(@"Couldn't Fetched the Data");
  
      }else{
  
          NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_receivedData options:NSJSONReadingMutableLeaves error:nil];
  
          if (dic==nil) {
  
  
              UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Could not load the menu" message:@"Do You want to Try Again ?" preferredStyle:UIAlertControllerStyleAlert];
              UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  
              }];
              [alert addAction:okAction];
              [self presentViewController:alert animated:YES completion:nil];
          }
          else{
  
  
  
  
             // [Model sharedInstance].arrayMenu = [[NSMutableArray alloc]init];
  
              NSArray *arrayData = [[NSArray alloc] initWithArray:[dic valueForKey:@"data"]];
  
              for (int i=0; i&lt;[arrayData count]; i++) {
  
               //   [[Model sharedInstance].arrayMenu addObject:[arrayData objectAtIndex:i]];
  
              }
          }
      }
  
  
  
  
  
  
  
  
  
  
  
      NSLog(@"Did Fail");
      //    [_activityView stopAnimating];
      //    _activityView.hidden=YES;
  
      [[UIApplication sharedApplication] endIgnoringInteractionEvents];
  
      UIAlertView *balle =[[UIAlertView alloc]initWithTitle:@"NO INTERNET" message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
      [balle show];
  }
  - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  {
      NSLog(@"Did Finish");
  
  
  
      NSError * error = nil;
      _responsedic = [NSJSONSerialization JSONObjectWithData:_responseData options:kNilOptions error:&amp;error];
  
      NSLog(@"%@",_responsedic);
  
      _arrname=[_responsedic valueForKey:@"profile"];
      NSLog(@"%@",_arrname);
         /*
       #pragma mark - Navigation
  
       // In a storyboard-based application, you will often want to do a little preparation before navigation
       - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
       // Get the new view controller using [segue destinationViewController].
       // Pass the selected object to the new view controller.
       }
       */
      [_profilecollectionview reloadData];
  
  }
  </code></pre>
  
  <p>Currently my screen looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/mgw7A.png" alt="Image 1"></p>
  
  <p>But I need to show like this</p>
  
  <p><img src="https://i.stack.imgur.com/LHFYp.jpg" alt="Image 2"></p>