iOS 有 快速语言的奖励插页式广告样本

  • 本文关键字:样本 语言 iOS ios swift admob
  • 更新时间 :
  • 英文 :


之前我在iOS应用中使用的是AdMob的banner广告和open app广告,现在我想展示的是它的reward inter插播广告。我查看了(AdMob的相关网页),但是这个页面的例子都是Objective C语言。我不熟悉Objective C语言。有人能提供Swift语言的指导吗?提前谢谢。

import UIKit
import GoogleMobileAds
class ViewController: UIViewController, GADFullScreenContentDelegate  {
@IBOutlet weak var btnRwdClick: UIButton!
@IBOutlet weak var btnInters: UIButton!
//var rewadAd: GADRewardedAd?
var rewardInterstitialAd: GADRewardedInterstitialAd?
var interstitial: GADInterstitialAd?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
GADRewardedInterstitialAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest()
) { (ad, error) in
if let error = error {
print("Rewarded ad failed to load with error: (error.localizedDescription)")
return
}
print("Loading Succeeded")
self.rewardInterstitialAd = ad
self.rewardInterstitialAd?.fullScreenContentDelegate = self
}
let request = GADRequest()
GADInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/4411468910",
request: request,
completionHandler: { [self] ad, error in
if let error = error {
print("Failed to load interstitial ad with error: (error.localizedDescription)")
return
}
interstitial = ad
interstitial?.fullScreenContentDelegate = self

}
)
}

@IBAction func rewadAdTouched(_ sender: Any) {
if let ad = rewardInterstitialAd {

ad.present(fromRootViewController: self,
userDidEarnRewardHandler: {
let reward = ad.adReward
// TODO: Reward the user.
print("Reward received with currency (reward.amount), amount (reward.amount.doubleValue)")
}
)
} else {
//Failed
}
}

@IBAction func interAdTouched(_ sender: Any) {
if interstitial != nil {
interstitial!.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
}
}
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Rewarded ad presented.")
}

func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad dismiss full screen ad")
if type(of: ad) == GADInterstitialAd.self {
print("InterstitialAd")

}else if  type(of: ad) == GADRewardedAd.self {
print("RewardedAd")
}else if  type(of: ad) == GADRewardedInterstitialAd.self {

print("Rewarded InterstitialAd")
}
}

func ad(
_ ad: GADFullScreenPresentingAd,
didFailToPresentFullScreenContentWithError error: Error
) {
print("Rewarded ad failed to present with error: (error.localizedDescription).")

}

}

下面是取自AdMob官方文档的一个快速示例

这是iOS插页广告的专用测试单元ID:ca-app-pub-3940256099942544/4411468910

加载广告

import GoogleMobileAds
import UIKit
class ViewController {
private var interstitial: GADInterstitialAd?
func viewDidLoad() {
super.viewDidLoad()
let request = GADRequest()
GADInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/4411468910",
request: request,
completionHandler: { [self] ad, error in
if let error = error {
print("Failed to load interstitial ad with error: (error.localizedDescription)")
return
}
interstitial = ad
}
)
}
}

注册回调

class ViewController: GADFullScreenContentDelegate {
private var interstitial: GADInterstitialAd?
func viewDidLoad() {
super.viewDidLoad()
let request = GADRequest()
GADInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/4411468910",
request: request,
completionHandler: { [self] ad, error in
if let error = error {
print("Failed to load interstitial ad with error: (error.localizedDescription)")
return
}
interstitial = ad
interstitial?.fullScreenContentDelegate = self
}
)
}
/// Tells the delegate that the ad failed to present full screen content.
func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("Ad did fail to present full screen content.")
}
/// Tells the delegate that the ad presented full screen content.
func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did present full screen content.")
}
/// Tells the delegate that the ad dismissed full screen content.
func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
print("Ad did dismiss full screen content.")
}
}

显示广告

@IBAction func doSomething(_ sender: Any) {
if interstitial != nil {
interstitial.present(fromRootViewController: self)
} else {
print("Ad wasn't ready")
}
}

相关内容

  • 没有找到相关文章

最新更新