我非常确定代码是正确的,但出于某种原因,广告在打开时并未显示在应用顶部。我正在迅速为我的IOS应用程序执行此操作,并希望广告显示在"主屏幕上#34;当你第一次打开应用程序时。
import UIKit
import GoogleMobileAds
class ViewController: UIViewController, GADBannerViewDelegate {
@IBOutlet var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
bannerView = GADBannerView(adSize: kGADAdSizeSmartBannerPortrait)
self.view.addSubview(bannerView)
bannerView.adUnitID = "ca-app-pub-1912006279271616/2057666171"
bannerView.rootViewController = self
bannerView.load(GADRequest())
let requestAd: GADRequest = GADRequest()
requestAd.testDevices = [kGADSimulatorID]
bannerView.load(requestAd)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
答案 0 :(得分:1)
下面的代码对我有用 - 我看到的唯一区别是设置frame.origin属性。也许它默认为(0,0)它可能隐藏在导航栏下,如果你的视图中有一个。尝试将横幅放在屏幕下方并检查。
let smart = kGADAdSizeSmartBannerPortrait
let banner = GADBannerView(adSize: smart)
banner.frame.origin = CGPoint(x: 0, y: offsetY) // set your own offset
banner.adUnitID = "ca-app-pub-........." // insert your own unit ID
banner.rootViewController = self
self.view.addSubview(banner)
let request = GADRequest()
banner.load(request)