swift使用google admod的横幅,插页式,激励式广告示例
广告都是代码创建的,代码如下:
import UIKit
import GoogleMobileAdsclass ViewController: UIViewController ,GADBannerViewDelegate ,GADInterstitialDelegate ,GADRewardBasedVideoAdDelegate {
var number = 0
var bannerView: GADBannerView!
var bannerView2: GADBannerView!
var interstitial: GADInterstitial!
@IBAction func jili_onClick(_ sender: Any) {if GADRewardBasedVideoAd.sharedInstance().isReady == true {
GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)
}}
@IBAction func onClick(_ sender: Any) {
number = number + 1
if number > 2 {
//提示本日次数已用完, 需要点击激励广告选项,可以增加2次
addTips()} else {
//弹个插页广告,然后执行正常流程
if interstitial.isReady {
interstitial.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
}
}}
override func viewDidLoad() {
super.viewDidLoad()//底部横幅广告
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
bannerView.rootViewController = self
bannerView.load(GADRequest())
bannerView.delegate = self
addBannerViewToView(bannerView)//顶部横幅广告
bannerView2 = GADBannerView(adSize: kGADAdSizeBanner)
bannerView2.adUnitID = "ca-app-pub-3940256099942544/2934735716"
bannerView2.rootViewController = self
bannerView2.load(GADRequest())
bannerView2.delegate = self
addBannerViewToViewTop(bannerView2)//插页式广告
interstitial = createAndLoadInterstitial()//激励式广告
GADRewardBasedVideoAd.sharedInstance().delegate = self
GADRewardBasedVideoAd.sharedInstance().load(GADRequest(),
withAdUnitID: "ca-app-pub-3940256099942544/1712485313")}
//激励式广告
//看完广告后会得到的奖励在这里处理,这里例子是会把number清零
func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd,
didRewardUserWith reward: GADAdReward) {
self.number = 0
}func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
GADRewardBasedVideoAd.sharedInstance().load(GADRequest(),
withAdUnitID: "ca-app-pub-3940256099942544/1712485313")
}//提示框
func addTips() {let alertController = UIAlertController(title: "系统提示",
message: "今日次数已用尽,点击Yes观看一个视频广告,奖励占卜2次", preferredStyle: .alert)let yesAction = UIAlertAction(title: "Yes", style: .default, handler: {
action in
//已用次数清0if GADRewardBasedVideoAd.sharedInstance().isReady == true {
GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)
}})
let noAction = UIAlertAction(title: "No", style: .cancel) {
(UIAlertAction) -> Void in//点击no,弹个插页广告
if self.interstitial.isReady {
self.interstitial.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
}}
alertController.addAction(yesAction)
alertController.addAction(noAction)self.present(alertController, animated: true, completion: nil)
}
//插页式广告
func createAndLoadInterstitial() -> GADInterstitial {
var interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
interstitial.delegate = self
interstitial.load(GADRequest())
return interstitial
}func interstitialDidDismissScreen(_ ad: GADInterstitial) {
interstitial = createAndLoadInterstitial()
}//底部横幅广告
func addBannerViewToView(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: bottomLayoutGuide,
attribute: .top,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
}//顶部横幅广告
func addBannerViewToViewTop(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .top,
relatedBy: .equal,
toItem: topLayoutGuide,
attribute: .bottom,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
}}
执行效果:

浙公网安备 33010602011771号