我正在尝试为应用内的所有bannerView配置单个广告。
在AppDelegate上:
class AppDelegate: UIResponder, UIApplicationDelegate, GADBannerViewDelegate {
var window: UIWindow?
var adBannerView = GADBannerView() // Create our one ADBannerView
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Initialize the Google Mobile Ads SDK.
GADMobileAds.configure(withApplicationID: "ca-app-pub-xxxxxx")
// Set delegate and hide banner initially
adBannerView.adUnitID = "ca-app-pub-xxxx"
adBannerView.delegate = self
adBannerView.clipsToBounds = true
adBannerView.load(GADRequest())
adBannerView.isHidden = true
return true
}
func adViewDidReceiveAd(_ bannerView: GADBannerView) {
adBannerView.isHidden = false
}
func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) {
adBannerView.isHidden = true
}
}
在ViewControllers viewDidLoad()
内部,我在下面调用函数:
func addBannerToView() {
appDelegate.adBannerView.adSize = kGADAdSizeBanner
appDelegate.adBannerView.rootViewController = self
appDelegate.adBannerView.frame = CGRect(x: 0.0,
y: view.frame.height - appDelegate.adBannerView.frame.height,
width: view.frame.width,
height: appDelegate.adBannerView.frame.height)
view.addSubview(appDelegate.adBannerView)
}
但我收到以下错误:
-Google-您必须设置-GADBannerView的rootViewController属性:0x7fed29701d70; frame =(0 0; 0 0); clipsToBounds = YES; layer = -CALayer:0x6080000296c0--在加载请求之前。