如何创建要在所有ViewControllers中显示的共享AdMob?

时间:2018-01-29 10:17:19

标签: ios swift compiler-errors admob

我正在尝试为应用内的所有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--在加载请求之前。

0 个答案:

没有答案