iOS 是否有 swift 语言的奖励插页式广告样本
iOS is there Rewarded interstitial Ad's Samples in swift language
之前,我在 iOS 应用中使用 AdMob 的横幅广告和打开应用广告。现在,我想展示它的奖励插页式广告。我检查了 (relevant web page of AdMob),但此页面上的示例全部使用 Objective C 语言。我不熟悉 Objective C 语言。谁能提供 Swift 语言的指导?提前致谢。
这里有一个 swift 例子取自 official AdMob documentation
这是 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")
}
}
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).")
}
}
之前,我在 iOS 应用中使用 AdMob 的横幅广告和打开应用广告。现在,我想展示它的奖励插页式广告。我检查了 (relevant web page of AdMob),但此页面上的示例全部使用 Objective C 语言。我不熟悉 Objective C 语言。谁能提供 Swift 语言的指导?提前致谢。
这里有一个 swift 例子取自 official AdMob documentation
这是 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")
}
}
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).")
}
}