Search code examples
iosswiftuitableviewadmob

integrating AdMob Native Ads Express in uitableviewcell?


i want to show Native Ads Express in uitableviewcell and i've created custom cell with custom class

import UIKit
import GoogleMobileAds

class GoogleAdsCell: UITableViewCell {

    @IBOutlet weak var NativeAds: GADNativeExpressAdView!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        NativeAds.layer.cornerRadius = 2
        NativeAds.layer.shadowOffset = CGSizeMake(0, 0)
        NativeAds.layer.shadowOpacity = 1.0
        NativeAds.layer.shadowRadius = 6
        NativeAds.clipsToBounds = true
    }

}

and in the viewController at cellForRowAtIndexPath

      let adsGoogleCell1 = tableView.dequeueReusableCellWithIdentifier("GoogleAdsCell") as! GoogleAdsCell 
      adsGoogleCell1.NativeAds.adUnitID = "ca-app-pub-3940256099942544/2562852117" 
      adsGoogleCell1.NativeAds.rootViewController = self 
      let request = GADRequest() 
      request.testDevices = [kGADSimulatorID] 
      adsGoogleCell1.NativeAds.loadRequest(request)
      return adsGoogleCell1

FIRST it works and the ads showed up but after some time it disappeared ?


Solution

  • I've faced exact same issue and solved by following official example from github. Here it is https://github.com/googleads/googleads-mobile-ios-examples/tree/master/Swift/admob/NativeExpressExample

    And this is complete code for my cellForRowAt method

    let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableBannerCell", for: indexPath) as! NewsDetailTableViewCell
    
    cell.newsBottomBannerContainer.adUnitID = "native_express_ads_banner_id"
    cell.newsBottomBannerContainer.rootViewController = self
    
    let videoOptions = GADVideoOptions()
    videoOptions.startMuted = true
    cell.newsBottomBannerContainer.setAdOptions([videoOptions])
    
    let request = GADRequest()
    request.testDevices = [kGADSimulatorID]
    cell.newsBottomBannerContainer.load(request)
    
    return cell