【问题标题】:add an ad banner in uiViewController in Swift3在 Swift3 的 uiViewController 中添加广告横幅
【发布时间】:2018-01-17 00:14:21
【问题描述】:

我想添加广告横幅,我使用了谷歌框架

pod 'Google-Mobile-Ads-SDK'

在我的 pod 文件中

我向我的 uiViewController > GoogleBannerView 添加了一个视图

这是我的代码

导入 GoogleMobileAds

在viewdidLoad中:

    GoogleBannerView.adUnitID = "ca-app-pub-5462393278951241/1172515484"
    GoogleBannerView.delegate = self
    GoogleBannerView.rootViewController = self
    GoogleBannerView.load(GADRequest())

但它不会在视图中显示任何内容并在控制台中给我一条消息:

要在此设备上获取测试广告,请调用:request.testDevices = @[ kGADSimulatorID ];

任何帮助表示赞赏! 谢谢..

【问题讨论】:

  • 检查 iPhone 一次,而不是模拟器 @ema 所以

标签: ios swift3 banner google-admob


【解决方案1】:

我会展示一个页脚 (320x50) 广告,如果有广告,该广告将从底部出现,如果停止投放,该广告会缓出。这是一个繁重的实现,如果您不想隐藏/显示空间,可以简化。

我的 UIViewController 有一个带有约束的硬编码视图(宽度为 320,高度为 50,水平居中,固定在安全区域的底部。我链接以下内容:

  • 广告视图到横幅视图
  • 对bannerViewHeightConstraint 的广告视图高度限制

请注意,在代码示例中,我使用了您的 adUnitID。当我们使用 DFP 时,我们的 ID 看起来有所不同。

import GoogleMobileAds

class SomeClass: UIViewController
{
    @IBOutlet weak var bannerView: DFPBannerView!
    @IBOutlet weak var bannerViewHeightConstraint: NSLayoutConstraint!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        bannerView.delegate = self
        bannerView.adUnitID = "ca-app-pub-5462393278951241/1172515484"
        bannerView.rootViewController = self
        bannerView.validAdSizes = [ NSValueFromGADAdSize(kGADAdSizeBanner) ]
        bannerView.translatesAutoresizingMaskIntoConstraints = false
    }

    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)
        bannerView.frame.origin.y += bannerView.frame.size.height
        bannerView.isHidden = true

        let request = GADRequest()
    //***** NOTE : per Google Documentation (https://developers.google.com/mobile-ads-sdk/docs/dfp/ios/test-ads)
    //        "iOS simulators are automatically configured as test devices."
//      request.testDevices = [ kGADSimulatorID ] //***** This was requried in previous versions, now on automatically done, don't think you can disable it.
        bannerView.load(request)
    }
}

extension SomeClass : GADBannerViewDelegate
{
    /// Tells the delegate an ad request loaded an ad.
    func adViewDidReceiveAd(_ bannerView: GADBannerView) {
        print("*** adViewDidReceiveAd for 'Banner' at \(Date())")
        if bannerView == self.bannerView
        {
            if bannerView.isHidden
            {
                bannerView.frame.origin.y += bannerView.frame.size.height
                bannerViewHeightConstraint.constant = 0
                bannerView.isHidden = false

                UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseIn, animations: {
                    bannerView.frame.origin.y -= bannerView.frame.size.height
                    self.bannerViewHeightConstraint.constant = 50
                }, completion: nil )
            }
        }
    }

    /// Tells the delegate an ad request failed.
    func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) {
        print("*** adView:didFailToReceiveAdWithError: \(error.localizedDescription) for 'Banner'")
        if bannerView == self.bannerView
        {
            print("Hidden: \(bannerView.isHidden)")
            if !bannerView.isHidden
            {
                UIView.animate(withDuration: 0.3, delay: 0.0, options: .curveEaseIn, animations: {
                    bannerView.frame.origin.y += bannerView.frame.size.height
                    self.bannerViewHeightConstraint.constant = 0
                }, completion: {finished in
                    if finished
                    {
                        bannerView.isHidden = true
                    }
                })
            }
        }
    }

    /// Tells the delegate that a full screen view will be presented in response
    /// to the user clicking on an ad.
    func adViewWillPresentScreen(_ bannerView: GADBannerView) {
        print("*** adViewWillPresentScreen for 'Banner'")
    }

    /// Tells the delegate that the full screen view will be dismissed.
    func adViewWillDismissScreen(_ bannerView: GADBannerView) {
        print("*** adViewWillDismissScreen for 'Banner'")
    }

    /// Tells the delegate that the full screen view has been dismissed.
    func adViewDidDismissScreen(_ bannerView: GADBannerView) {
        print("*** adViewDidDismissScreen for 'Banner'")
    }

    /// Tells the delegate that a user click will open another app (such as
    /// the App Store), backgrounding the current app.
    func adViewWillLeaveApplication(_ bannerView: GADBannerView) {
        print("*** adViewWillLeaveApplication for 'Banner'")
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多