我正在我的应用中为单个UIViewController
实施AdMob横幅,并且它正在运行。但我有很多观点,我想在每个屏幕上显示一个横幅。如何实现每个屏幕上都会出现的横幅?我在我的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)
})
但它不起作用
答案 0 :(得分:3)
创建共享横幅。您init
中AppDelegate
,然后将其添加到您想要横幅广告的UIViewController
中:
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
中,您想要一个横幅:
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)
}
答案 1 :(得分:0)
SWIFT 3
func showAds(viewAds:GADBannerView,viewController:UIViewController){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.adBannerView.adSize = kGADAdSizeBanner
appDelegate.adBannerView.rootViewController = viewController
appDelegate.adBannerView.frame = CGRect(x: 0,
y: 0,
width: viewAds.frame.width,
height: viewAds.frame.height)
viewAds.addSubview(appDelegate.adBannerView)
}
showAds(viewAds: adseHere, viewController: self)