[英]add an ad banner in uiViewController in Swift3
I want to add and ad banner, I used google framework 我想添加广告横幅,我使用了Google框架
pod 'Google-Mobile-Ads-SDK'
in my pod file 在我的pod文件中
I added a view to my uiViewController > GoogleBannerView 我在uiViewController> GoogleBannerView中添加了一个视图
this is my code 这是我的代码
import GoogleMobileAds 导入GoogleMobileAds
in the viewdidLoad: 在viewdidLoad中:
GoogleBannerView.adUnitID = "ca-app-pub-5462393278951241/1172515484"
GoogleBannerView.delegate = self
GoogleBannerView.rootViewController = self
GoogleBannerView.load(GADRequest())
But it doesn't display anything in the view and gives me a message in the console: 但是它在视图中不显示任何内容,并在控制台中显示了一条消息:
To get test ads on this device, call: request.testDevices = @[ kGADSimulatorID ]; 要在此设备上获取测试广告,请致电:request.testDevices = @ [kGADSimulatorID];
Any help is appreciated! 任何帮助表示赞赏! Thanks..
谢谢..
I display a footer (320x50) ad that will come up from the bottom if there is one and ease out if there stops being one to serve. 我显示了一个页脚(320x50)广告,如果有一个广告,它将从底部开始显示;如果不再有一个广告,则从底部开始显示。 This is a heavy implementation and could be simplified if you didn't want to hide/show the space.
这是一个繁重的实现,如果您不想隐藏/显示空间,则可以简化。
My UIViewController has a hard coded view with constraints (320 width, 50 height, centered Horizontal, pinned to bottom of safe area. I link the following: 我的UIViewController有一个硬编码视图,带有约束(320宽度,50高度,水平居中,固定在安全区域底部)。我链接了以下内容:
Note that in the code sample I use your adUnitID.
请注意,在代码示例中,我使用了您的adUnitID。 Our ID looks different as we are using DFP.
使用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'")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.