Search code examples
iosobjective-cmopubfacebook-audience-network

MoPub How to implement iOS Native Ads using MPTableViewAdPlacer with dynamic cell height?


I am using a MPTableViewAdPlacer to implement native ads in iOS UITableView. When initialising the MPTableViewAdPlacer, it asks for a MPStaticNativeAdRendererSettings, which needs to implement a viewSizeHandler. However, this is before any ads are fetched, as the name suggested "Static" Native Ad. I am trying to implement one where the cell height can be calculated after getting the adData, such as the title, image...etc. I've been trying to find a way to implement a dynamic cell height but all the sample app, instructions provided by twitter only shows the static height implementation.

Code below:

-(void)setupAdPlacer {

    MPNativeAdRequestTargeting *targeting = [MPNativeAdRequestTargeting targeting];
    targeting.location = [[CLLocationManager alloc] init].location;

    targeting.desiredAssets = [NSSet setWithObjects: kAdMainImageKey, kAdCTATextKey, kAdTextKey, kAdTitleKey, nil];

    MPStaticNativeAdRendererSettings *settings = [[MPStaticNativeAdRendererSettings alloc] init];

    settings.renderingViewClass = [REPostListViewMoPubAdCell class];
    settings.viewSizeHandler = ^(CGFloat maximumWidth) {
    return CGSizeMake(maximumWidth, 312.0);

    // STATIC HEIGHT
    };

    MPNativeAdRendererConfiguration *config = [MPStaticNativeAdRenderer rendererConfigurationWithRendererSettings:settings];

    self.adPlacer = [MPTableViewAdPlacer placerWithTableView:self.tableView viewController:self adPositioning:positioning rendererConfigurations:@[config]];
    self.adPlacer.delegate = self;

    [self.adPlacer loadAdsForAdUnitID:@"xxxxxxxxxxx" targeting:targeting];
}

Solution

  • After contacting Twitter tech support, I confirmed that currently iOS does not support dynamic cell height. The only way is to size your views according to the best practice as @Vatsal K pointed out in his comments above.

    enter image description here