问题描述
我正在我的应用程序中为单个 UIViewController
实现 AdMob 横幅,它正在工作.但是我有很多视图,我想在每个屏幕上显示一个横幅.我将如何实现将出现在每个屏幕上的横幅?我正在我的 AppDelegate.swift
中尝试这个:
I am implementing an AdMob banner in my app for a single UIViewController
, and it is working. But I have so many views and I want to show a banner on every screen. How would I implement a banner that would appear on every screen? I am trying this in my AppDelegate.swift
:
dispatch_async(dispatch_get_main_queue(), {
print("Google Mobile Ads SDK version: (GADRequest.sdkVersion())")
// bannerView.frame = CGRectMake(0, 0, 320, 50)
// self.bannerView.adSize = kGADAdSizeBanner
self.bannerView.adUnitID = "ca-app-pub-MY_ID"
bannerView.rootViewController = self.window
self.bannerView.loadRequest(GADRequest())
self.window?.addSubview(self.bannerView)
})
但它不起作用
推荐答案
创建共享横幅.您在 AppDelegate
中对其 init
它,然后将其添加到您希望在其上放置横幅的 UIViewController
中:
Create a shared banner. You init
it in your AppDelegate
and then add it to the UIViewController
's that you'd like to have a banner on:
class AppDelegate: UIResponder, UIApplicationDelegate, GADBannerViewDelegate {
var window: UIWindow?
var adBannerView = GADBannerView()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
adBannerView.adUnitID = "YourAdUnitID"
adBannerView.delegate = self
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
}
您希望在每个 UIViewController
中都有一个横幅:
In each UIViewController
you'd like to have a banner:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
addBannerToView()
}
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)
}
这篇关于如何将 AdMob GADBannerView 添加到每个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!