Facebook native Ads are not clickable again in UITableView Cells after scroll
Asked Answered
P

3

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
Prototherian answered 9/6, 2018 at 5:21 Comment(0)
T
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
Tetrabrach answered 21/8, 2018 at 17:41 Comment(0)
T
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.

Talyah answered 4/7, 2018 at 11:56 Comment(1)
I am using custom cell for displaying ads, Your code is for default cell.Prototherian
T
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
Tetrabrach answered 21/8, 2018 at 17:41 Comment(0)
I
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.

Intergrade answered 1/7, 2018 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.