我想显示原生广告Express在uitableviewcell我用自定义类
创建了自定义单元格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
}
}
和viewController中的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
一开始它工作,广告显示,但一段时间后它消失了?
我遇到过完全相同的问题,并通过github的官方示例解决了这个问题。这里是https://github.com/googleads/googleads-mobile-ios-examples/tree/master/Swift/admob/NativeExpressExample
这是cellForRowAt方法的完整代码
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