6

I have implemented facebook native Ads in UITableView, for first 1-2 times it clickable but when I scroll tableview and come again back to the same cell, now Ads are not clicking, I am using swift 3.2 Below is the cell implementation.

let ad = adsManager.nextNativeAd
let cell = self.tableHome.dequeueReusableCell(withIdentifier: "HomeAdsTableViewCell", for: indexPath) as! HomeAdsTableViewCell
cell.message.text = ad?.body
cell.title.text = ad?.title
cell.callToActionButton.setTitle(ad?.callToAction, for: .normal)

if let pic = ad?.coverImage {
    cell.postImage.setImageWithIndicator(imageUrl:pic.url.absoluteString)
}

ad?.registerView(forInteraction: cell.postView, with: self)
cell.selectionStyle=UITableViewCellSelectionStyle.none

return cell
Iraniya Naynesh
  • 1,025
  • 3
  • 14
  • 22

3 Answers3

1

I suggest you follow the steps here. He told me about putting a facebook ad between cells.

https://www.appcoda.com/facebook-ads-integration/

It's like you do not make mistakes in the place you turn the idiot.

You should adapt this part in the link to your own.

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
       if adsCellProvider != nil && adsCellProvider.isAdCellAtIndexPath(indexPath, forStride: UInt(adRowStep)) {
         return adsCellProvider.tableView(tableView, cellForRowAtIndexPath: indexPath)
       }
       else {
         let cell = tableView.dequeueReusableCellWithIdentifier("idCellSample", forIndexPath: indexPath) as! SampleCell
         cell.lblTitle.text = sampleData[indexPath.row - Int(indexPath.row / adRowStep)]
         return cell
       }  }

You should also use this method on screen refreshes and turns.

    func configureAdManagerAndLoadAds() {
      if adsManager == nil {
        adsManager = FBNativeAdsManager(placementID: "PLACEMENT_ID", forNumAdsRequested: 5)
        adsManager.delegate = self
        adsManager.loadAds()
      }}
    override func viewWillAppear(animated: Bool) {
      configureAdManagerAndLoadAds()}

Finally you should check the relevant fields, which may not be compatible with content swift 3.

1
//create a new object  of nativead in your class//

var previousNativead : FBNativeAd?

let ad = adsManager.nextNativeAd
self.previousNativead?.unregisterView()
let cell = self.tableHome.dequeueReusableCell(withIdentifier: "HomeAdsTableViewCell", for: indexPath) as! HomeAdsTableViewCell
cell.message.text = ad?.body
cell.title.text = ad?.title
cell.callToActionButton.setTitle(ad?.callToAction, for: .normal)

if let pic = ad?.coverImage {
    cell.postImage.setImageWithIndicator(imageUrl:pic.url.absoluteString)
}
previousNativead = ad
ad?.registerView(forInteraction: cell.postView, with: self)
cell.selectionStyle=UITableViewCellSelectionStyle.none

return cell
0

First in your viewDidLoad() check that you add

override func viewDidLoad() {
     super.viewDidLoad()
     adsManager = FBNativeAdsManager(placementID: "YOUR_PLACEMENT_ID_HERE", forNumAdsRequested: "YourNumAds")
     adsManager.delegate = self
     adsManager.loadAds()
}

then to comply with the FBNativeAdsManagerDelegate, you will need to add the following method, nativeAdsLoaded().

    func nativeAdsLoaded() { 
    adsCellProvider = FBNativeAdTableViewCellProvider(manager: adsManager, forType: FBNativeAdViewType.genericHeight300)
    adsCellProvider.delegate = self
    if self.tableview != nil {
         self.tableview.reloadData()
       }
     }

the last thing you should do is to registerView directly to the cell not to postView

ad?.registerView(forInteraction: cell, with: self)

hope that will help you.

A.Munzer
  • 1,330
  • 11
  • 24