ios - iOS 有没有 Swift 语言的 Rewarded interstitial Ad's Samples
问题描述
之前,我在 iOS 应用中使用了 AdMob 的横幅广告和打开应用广告。现在,我想展示它的奖励式插页式广告。我查看了(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).")
}
}
推荐阅读
- gnuplot - 你如何对 gnuplot 中的每 n 列数据求和?
- python - Python 的 select() 延迟报告子进程输出的存在
- javascript - 如何在 Javascript 中创建数学上正确且可接受的分区
- python - 创建一个复杂的循环
- r - 在 Rstudio 中安装 R 闪亮出现错误
- python - 在python中查找轨迹时间序列的周期性
- asp.net - Linq Enity Framework,从 IQueryable 导航属性中的集合中选择
- php - 如何将获取值 php 添加到引导切换折叠中?
- javascript - 调整 scrollIntoView 的持续时间
- python - IndexError:for循环python中的列表索引超出范围