Ödüllü geçiş reklamı, doğal uygulama geçişleri sırasında otomatik olarak görünen reklamlar için ödül sunmanıza olanak tanıyan teşvik edici bir reklam biçimidir. Ödüllü reklamların aksine kullanıcıların ödüllü geçiş reklamı görüntülemeyi etkinleştirmesi gerekmez.
Ön koşullar
- Google Mobile Ads SDK'sı 7.60.0 veya daha sonraki bir sürüm
- Başlangıç kılavuzunu tamamlayın.
Uygulama
Ödüllü geçiş reklamlarını entegre etme ile ilgili temel adımlar şunlardır:
- Reklam yükleme
- [İsteğe bağlı] SSV geri çağırmalarını doğrulama
- Geri arama için kaydolma
- Reklamı gösterin ve ödül etkinliğini işleyin
Reklam yükleme
Reklam yükleme işlemi, GADRewardedInterstitialAd
sınıfındaki load(adUnitID:request)
yöntemi kullanılarak gerçekleştirilir.
Swift
import GoogleMobileAds
import UIKit
class ViewController: UIViewController {
private var rewardedInterstitialAd: RewardedInterstitialAd?
override func viewDidLoad() {
super.viewDidLoad()
Task {
do {
rewardedInterstitialAd = try await RewardedInterstitialAd.load(
with: "ca-app-pub-3940256099942544/6978759866", request: Request())
} catch {
print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
}
}
}
}
SwiftUI
import GoogleMobileAds
class RewardedInterstitialViewModel: NSObject, ObservableObject,
FullScreenContentDelegate
{
@Published var coins = 0
private var rewardedInterstitialAd: RewardedInterstitialAd?
func loadAd() async {
do {
rewardedInterstitialAd = try await RewardedInterstitialAd.load(
with: "ca-app-pub-3940256099942544/6978759866", request: Request())
rewardedInterstitialAd?.fullScreenContentDelegate = self
} catch {
print(
"Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
}
}
Objective-C
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, strong) GADRewardedInterstitialAd* rewardedInterstitialAd;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[GADRewardedInterstitialAd
loadWithAdUnitID:@"<var label='the ad unit ID'>ca-app-pub-3940256099942544/6978759866</var>"
request:[GADRequest request]
completionHandler:^(
GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd,
NSError* _Nullable error) {
if (!error) {
self.rewardedInterstitialAd = rewardedInterstitialAd;
}
}
];
}
[İsteğe bağlı] Sunucu tarafı doğrulama (SSV) geri çağırmalarını doğrulama
Sunucu tarafı doğrulama geri çağırmalarında ek veriler gerektiren uygulamalar, ödüllü reklamların özel veri özelliğini kullanmalıdır. Bir ödüllü reklam nesnesinde ayarlanan tüm dize değerleri, SSV geri çağırmasının custom_data
sorgu parametresine iletilir. Özel veri değeri ayarlanmamışsa custom_data
sorgu parametresi değeri SSV geri çağırmasında bulunmaz.
Aşağıdaki kod örneği, reklam isteğinde bulunmadan önce ödüllü geçiş reklamı nesnesinde özel verilerin nasıl ayarlanacağını gösterir.
Swift
do {
rewardedInterstitialAd = try await RewardedInterstitialAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: Request())
let options = ServerSideVerificationOptions()
options.customRewardText = "SAMPLE_CUSTOM_DATA_STRING"
rewardedInterstitialAd.serverSideVerificationOptions = options
} catch {
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
}
Objective-C
[GADRewardedInterstitialAd
loadWithAdUnitID:@"ca-app-pub-3940256099942544/6978759866"
request:GADRequest
completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) {
if (error) {
// Handle Error
return;
}
self.rewardedInterstitialAd = ad;
GADServerSideVerificationOptions *options =
[[GADServerSideVerificationOptions alloc] init];
options.customRewardString = @"SAMPLE_CUSTOM_DATA_STRING";
ad.serverSideVerificationOptions = options;
}];
Geri arama için kaydolma
Sunum etkinlikleriyle ilgili bildirim almak için GADFullScreenContentDelegate
protokolünü uygulamanız ve döndürülen reklamın fullScreenContentDelegate
özelliğine atamanız gerekir. GADFullScreenContentDelegate
protokolü, reklamın başarıyla veya başarısızlıkla sunulduğu ve kapatıldığı zamanlardaki geri çağırmaları işler. Aşağıdaki kodda, protokolün nasıl uygulanacağı ve reklama nasıl atanacağı gösterilmektedir:
Swift
import GoogleMobileAds
import UIKit
class ViewController: UIViewController, FullScreenContentDelegate {
private var rewardedInterstitialAd: RewardedInterstitialAd?
override func viewDidLoad() {
super.viewDidLoad()
Task {
do {
rewardedInterstitialAd = try await RewardedInterstitialAd.load(
with: "ca-app-pub-3940256099942544/6978759866", request: Request())
self.rewardedInterstitialAd?.fullScreenContentDelegate = self
} catch {
print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
}
}
}
/// Tells the delegate that the ad failed to present full screen content.
func ad(_ ad: FullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
print("Ad did fail to present full screen content.")
}
/// Tells the delegate that the ad will present full screen content.
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
print("Ad will present full screen content.")
}
/// Tells the delegate that the ad dismissed full screen content.
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("Ad did dismiss full screen content.")
}
}
SwiftUI
Döndürülen reklama fullScreenContentDelegate
özelliğini atayın:
rewardedInterstitialAd?.fullScreenContentDelegate = self
Protokolü uygulayın:
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func adDidRecordClick(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func ad(
_ ad: FullScreenPresentingAd,
didFailToPresentFullScreenContentWithError error: Error
) {
print("\(#function) called")
}
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
}
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
print("\(#function) called")
// Clear the rewarded interstitial ad.
rewardedInterstitialAd = nil
}
Objective-C
@interface ViewController () <GADFullScreenContentDelegate>
@property(nonatomic, strong) GADRewardedInterstitialAd *rewardedInterstitialAd;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[GADRewardedInterstitialAd
loadWithAdUnitID:@"ca-app-pub-3940256099942544/6978759866"
request:[GADRequest request]
completionHandler:^(
GADRewardedInterstitialAd *_Nullable rewardedInterstitialAd,
NSError *_Nullable error) {
if (!error) {
self.rewardedInterstitialAd = rewardedInterstitialAd;
self.rewardedInterstitialAd.fullScreenContentDelegate = self;
}
}];
}
/// Tells the delegate that the ad failed to present full screen content.
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
NSLog(@"Ad did fail to present full screen content.");
}
/// Tells the delegate that the ad will present full screen content.
- (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"Ad will present full screen content.");
}
/// Tells the delegate that the ad dismissed full screen content.
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
NSLog(@"Ad did dismiss full screen content.");
}
Reklamı gösterin ve ödül etkinliğini işleyin
Reklamınızı sunarken, kullanıcıya ödül vermek için bir GADUserDidEarnRewardHandler
nesnesi sağlamanız gerekir.
Aşağıdaki kod, ödüllü geçiş reklamı göstermenin en iyi yöntemini sunar.
Swift
func show() {
guard let rewardedInterstitialAd = rewardedInterstitialAd else {
return print("Ad wasn't ready.")
}
// The UIViewController parameter is an optional.
rewardedInterstitialAd.present(from: nil) {
let reward = rewardedInterstitialAd.adReward
print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
// TODO: Reward the user.
}
}
SwiftUI
Reklamı görüntülemek için görünümdeki kullanıcı arayüzü etkinliklerini dinleyin.
var rewardedInterstitialBody: some View {
// ...
}
.onChange(
of: showAd,
perform: { newValue in
if newValue {
viewModel.showAd()
}
}
)
Ödüllü geçiş reklamını görünüm modelinden sunun:
func showAd() {
guard let rewardedInterstitialAd = rewardedInterstitialAd else {
return print("Ad wasn't ready.")
}
rewardedInterstitialAd.present(from: nil) {
let reward = rewardedInterstitialAd.adReward
print("Reward amount: \(reward.amount)")
self.addCoins(reward.amount.intValue)
}
}
Objective-C
- (void)show {
// The UIViewController parameter is nullable.
[_rewardedInterstitialAd presentFromRootViewController:nil
userDidEarnRewardHandler:^{
GADAdReward *reward =
self.rewardedInterstitialAd.adReward;
// TODO: Reward the user.
}];
}
GitHub'daki örnekler
Ödüllü geçiş reklamı örneklerinin tamamını tercih ettiğiniz dilde görüntüleyin:
Sonraki adımlar
Kullanıcı gizliliği hakkında daha fazla bilgi edinin.