Reklama pełnoekranowa z nagrodą reklam opartych na zachętach, które pozwalają oferować nagrody za wyświetlane reklamy automatycznie podczas naturalnych przechodzenia między aplikacjami. W przeciwieństwie do reklam z nagrodą użytkownicy wymagane do wyświetlenia reklamy pełnoekranowej z nagrodą.
Wymagania wstępne
- Pakiet SDK do reklam mobilnych Google w wersji 7.60.0 lub nowszej.
- Zapoznaj się z przewodnikiem dla początkujących.
Implementacja
Aby zintegrować reklamy pełnoekranowe z nagrodą, musisz wykonać te podstawowe czynności:
- Wczytywanie reklamy
- [Opcjonalnie] Zweryfikuj wywołania zwrotne SSV
- Zarejestruj się na wywołania zwrotne
- Wyświetl reklamę i obsługuje zdarzenie nagrody
Wczytywanie reklamy
Reklamę można wczytać za pomocą statycznego parametru
loadWithAdUnitID:request:completionHandler:
na
GADRewardedInterstitialAd
zajęcia. Metoda wczytywania wymaga identyfikatora jednostki reklamowej,
obiekt GADRequest
i moduł obsługi ukończenia.
który jest wywoływany, gdy reklama zostanie wczytana pomyślnie lub nie. Wczytany
W komponencie podany jest obiekt GADRewardedInterstitialAd
jako parametr
. Z przykładu poniżej dowiesz się, jak wczytać element GADRewardedInterstitialAd
w
ViewController
.
Swift
import GoogleMobileAds
import UIKit
class ViewController: UIViewController {
private var rewardedInterstitialAd: GADRewardedInterstitialAd?
override func viewDidLoad() {
super.viewDidLoad()
do {
rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
} 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;
}
}
];
}
[Opcjonalnie] Weryfikacja wywołań zwrotnych weryfikacji po stronie serwera (SSV)
Aplikacje, które wymagają dodatkowych danych po stronie serwera
weryfikacji powinny używać funkcji
funkcji danych niestandardowych w przypadku reklam z nagrodą. Dowolny ciąg znaków ustawiony w reklamie z nagrodą
jest przekazywany do parametru zapytania custom_data
w wywołaniu zwrotnym SSV. Jeśli nie
niestandardowych wartości danych, wartość parametru zapytania custom_data
nie będzie
w wywołaniu zwrotnym SSV.
Przeanalizuj przykładowy kod poniżej pokazujący, jak skonfigurować dane niestandardowe w przypadku reklamy z nagrodą pełnoekranowej reklamy przed wysłaniem żądania reklamy.
Swift
do {
rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
let options = GADServerSideVerificationOptions()
options.customRewardString = "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;
}];
Zarejestruj się na wywołania zwrotne
Aby otrzymywać powiadomienia o wydarzeniach prezentacji, musisz zaimplementować
protokół GADFullScreenContentDelegate
i przypisz go do
właściwość fullScreenContentDelegate
zwróconej reklamy.
Protokół GADFullScreenContentDelegate
obsługuje wywołania zwrotne, gdy reklama
wyświetla się prawidłowo lub nieskutecznie oraz kiedy jest zamykany. Poniżej
pokazuje, jak wdrożyć protokół i przypisać go do reklamy:
Swift
import GoogleMobileAds
import UIKit
class ViewController: UIViewController, GADFullScreenContentDelegate {
private var rewardedInterstitialAd: GADRewardedInterstitialAd?
override func viewDidLoad() {
super.viewDidLoad()
do {
rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
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: GADFullScreenPresentingAd, 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: GADFullScreenPresentingAd) {
print("Ad will 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.")
}
}
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.");
}
Wyświetl reklamę i obsługuje zdarzenie nagrody
Gdy wyświetlasz reklamę, musisz podać obiekt GADUserDidEarnRewardHandler
aby obsługiwać nagrodę dla użytkownika.
Poniższy kod przedstawia najlepszą metodę wyświetlania reklamy z nagrodą reklama pełnoekranowa.
Swift
func show() {
guard let rewardedInterstitialAd = rewardedInterstitialAd else {
return print("Ad wasn't ready.")
}
// The UIViewController parameter is an optional.
rewardedInterstitialAd.present(fromRootViewController: nil) {
let reward = rewardedInterstitialAd.adReward
print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
// TODO: Reward the user.
}
}
Objective-C
- (void)show {
// The UIViewController parameter is nullable.
[_rewardedInterstitialAd presentFromRootViewController:nil
userDidEarnRewardHandler:^{
GADAdReward *reward =
self.rewardedInterstitialAd.adReward;
// TODO: Reward the user.
}];
}
Dalsze kroki
Dowiedz się więcej o prywatności użytkowników.