0

I am trying to add Native Ads using Facebook SDK (FBAudienceNetwork.framework).

I found a nice tutorial in Facebook SDK itself. But my question is while inserting ads in Tableview, the indexpath gets increased. When selecting cell in didSelectRowAtIndexpath:tableview I am getting an increased indexpath (Wrong one).

What should I do to get the exact indexpath of the cell?

enter image description here

Output : Indexpath = 0,2

enter image description here

Output : Indexpath = 0,3

Any relevant help and suggestions appreciated. Cheers!

Maniganda saravanan
  • 2,089
  • 1
  • 17
  • 32

2 Answers2

0

The right way to integrate FBNativeads is to use the FBNativeAdTableViewCellProvider.h class with [self.adsCellProvider adjustCount:self.objects.count forStride:adRowStep];

IMPLEMENTATION

1) Declarations in .h

    @interface TableviewViewController : UITableViewController<FBNativeAdsManagerDelegate>{

 int adRowStep;
}

    @property (strong, nonatomic) FBNativeAd *nativeAd;
    @property (strong, nonatomic) FBNativeAdsManager*adsManager;
    @property (retain, nonatomic) FBNativeAdTableViewCellProvider*adsCellProvider;

2) ViewDidLoad (You can custom how many ads you want) under forNumAdsRequested

-(void)viewDidLoad{

self.adsManager = [[FBNativeAdsManager alloc]initWithPlacementID:@"PLACEMENT_ID" forNumAdsRequested:3];  

self.adsManager.delegate = self;
[self.adsManager loadAds];

}

3) Native Ads Loaded from FBNativeAdManager

-(void)nativeAdsLoaded{
    self.adsCellProvider = [[FBNativeAdTableViewCellProvider alloc]initWithManager:self.adsManager forType:FBNativeAdViewTypeGenericHeight120];

    self.adsCellProvider.delegate = self;

    if (self.tableView != nil) {
        [self.tableView reloadData];
    }
}

- (void)nativeAd:(FBNativeAd *)nativeAd didFailWithError:(NSError *)error
{
    NSLog(@"Native ad failed to load with error: %@", error);
}

- (void)nativeAdDidClick:(FBNativeAd *)nativeAd
{
    NSLog(@"Native ad was clicked.");
}

- (void)nativeAdDidFinishHandlingClick:(FBNativeAd *)nativeAd
{
    NSLog(@"Native ad did finish click handling.");
}

- (void)nativeAdWillLogImpression:(FBNativeAd *)nativeAd
{
    NSLog(@"Native ad impression is being captured.");
}

4) UITableview Sections + Rows Setups

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


        if (self.adsCellProvider != nil) {

            return [self.adsCellProvider adjustCount:self.objects.count forStride:adRowStep];
        }

        else{

            return self.objects.count;
        }


    return 0;
}

5) Return the different cells in order

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        CustomTableViewCell*cell;

     if (self.adsCellProvider != nil && [self.adsCellProvider isAdCellAtIndexPath:indexPath forStride:adRowStep]) {

                cell = [self.tableView dequeueReusableCellWithIdentifier:@"facebook_cell" forIndexPath:indexPath];


                FBNativeAd*nativeAdx = self.adsManager.nextNativeAd;


                if (nativeAdx) {
                    [nativeAdx unregisterView];
                }


                NSURL*image_url =  nativeAdx.coverImage.url;
                NSURL*image_icon_url = nativeAdx.icon.url;


                dispatch_async(dispatch_get_global_queue(0,0), ^{
                    NSData * data = [[NSData alloc] initWithContentsOfURL: image_url];
                    NSData* data2 = [[NSData alloc] initWithContentsOfURL:image_icon_url];

                    if ( data == nil && data2 == nil)
                        return;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        // WARNING: is the cell still using the same data by this point??
                        cell.adCoverMediaView.image = [UIImage imageWithData:data];

                        cell.adIconImageView.image = [UIImage imageWithData:data2];

                    });

                });



                // Render native ads onto UIView
                cell.adTitleLabel.text = nativeAdx.title;
                cell.adBodyLabel.text = nativeAdx.body;
                cell.adSocialContextLabel.text = nativeAdx.socialContext;
                cell.sponsoredLabel.text = @"Sponsored";

                [cell.adCallToActionButton setTitle:nativeAdx.callToAction
                                           forState:UIControlStateNormal];



                cell.adChoicesView.nativeAd = nativeAdx;
                cell.adChoicesView.corner = UIRectCornerTopRight;


                [nativeAdx registerViewForInteraction:cell withViewController:self];


                return cell;
            }
            else{


                long dif = indexPath.row - (indexPath.row / adRowStep);

                 NSDictionary*object = [self.objects objectAtIndex:dif];

                 //Do whatever you want with your object + create his own cell
// This is not a Facebook Ad Cell


            }

    }

6) DidSelectRowAtIndexPath --> Answering your question with this schema

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

if (self.adsCellProvider != nil && [self.adsCellProvider isAdCellAtIndexPath:indexPath forStride:adRowStep]) {



        }
        else{


            long dif = indexPath.row - (indexPath.row / adRowStep);

            PFObject*sender_obx = [self.objects objectAtIndex:dif];

            PostDetailsViewController*rootViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"post_detail_VC"];
            rootViewController.post_obx = sender_obx;
            rootViewController.post_id  = sender_obx.objectId;

            [self.navigationController pushViewController:rootViewController animated:YES];

        }

}

This is the way I do it. I hope it will help you

Kingofmit
  • 1,946
  • 1
  • 15
  • 22
-1

Actually I will subclass the UITableViewCell class and add a model property to it.In TableView's

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SubCellClass *cell = [tableView dequeueReusableCellWithIdentifier:cellName forIndexPath:indexPath];
    cell.model = [self.data objectAtIndex:indexPath.row];
    ...
}

method, I will set model property.

And when I select the cell, I could just call cell.model and get the data directly.

Meet Doshi
  • 3,983
  • 10
  • 35
  • 76
Andrew Carl
  • 742
  • 1
  • 5
  • 14
  • Thanks for the reply. But my question is to get cell you need correct selected indexpath right? Here indexpath is changing after adding ads in tableview. Thats the problem. Please refer images. – Maniganda saravanan Aug 28 '16 at 07:51
  • Is it possible to get ad Title, icon, text separately and display in our design in the table view cell. – Pramod Tapaniya Feb 28 '17 at 05:44