2

我正在我的应用程序中为单个 实现 AdMob 横幅UIViewController,并且它正在工作。但是我有很多视图,我想在每个屏幕上显示一个横幅。我将如何实现将出现在每个屏幕上的横幅?我正在尝试这个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)
})

但它不工作

4

2 回答 2

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)
}
于 2016-09-19T14:38:47.003 回答
0

斯威夫特 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)
于 2017-06-19T09:51:26.967 回答