L'interstitial con premio è un tipo di formato dell'annuncio con incentivi che ti consente di offrire premi per gli annunci che vengono visualizzati automaticamente durante le normali transizioni dell'app. A differenza degli annunci con premio, gli utenti non devono attivare la visualizzazione degli annunci interstitial con premio.
Prerequisiti
Prima di continuare, configura Google Mobile Ads SDK.
Implementazione
I passaggi principali per l'integrazione degli annunci interstitial con premio sono i seguenti:
- Carica un annuncio
- [Facoltativo] Convalida i callback SSV
- Registrati per i callback
- Mostra l'annuncio e gestisci l'evento premio
Carica un annuncio
Il caricamento di un annuncio viene eseguito utilizzando il metodo load(adUnitID:request) nella classe GADRewardedInterstitialAd.
Swift
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
Sostituisci adUnitID con l'ID unità pubblicitaria.
[Facoltativo] Convalida i callback di verifica lato server (SSV)
Le app che richiedono dati aggiuntivi nei callback di verifica lato server devono utilizzare la funzionalità dei dati personalizzati degli annunci con premio. Qualsiasi valore di stringa impostato su un oggetto annuncio con premio viene passato al parametro di query custom_data del callback SSV. Se non viene impostato alcun valore dei dati personalizzati, il valore del parametro di query custom_data non sarà presente nel callback SSV.
Il seguente esempio di codice mostra come impostare dati personalizzati su un oggetto annuncio interstitial con premio prima di richiedere un annuncio.
Swift
Objective-C
Sostituisci SAMPLE_CUSTOM_DATA_STRING con i tuoi dati personalizzati.
Registrati per i callback
Per ricevere notifiche per gli eventi di presentazione, devi assegnare
GADFullScreenContentDelegate alla proprietà fullScreenContentDelegate
dell'annuncio restituito:
Swift
rewardedInterstitialAd?.fullScreenContentDelegate = self
SwiftUI
rewardedInterstitialAd?.fullScreenContentDelegate = self
Objective-C
self.rewardedInterstitialAd.fullScreenContentDelegate = self;
Il protocollo GADFullScreenContentDelegate gestisce i callback per i casi in cui l'annuncio viene presentato correttamente o meno e quando viene ignorato. Il seguente codice mostra come implementare il protocollo e assegnarlo all'annuncio:
Swift
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
print("\(#function) called.")
}
func adDidRecordClick(_ ad: FullScreenPresentingAd) {
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
}
func ad(
_ ad: FullScreenPresentingAd,
didFailToPresentFullScreenContentWithError error: Error
) {
print("\(#function) called with error: \(error.localizedDescription).")
}
SwiftUI
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
- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adWillDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}
- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
// Clear the rewarded interstitial ad.
self.rewardedInterstitialAd = nil;
}
- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
}
Mostra l'annuncio e gestisci l'evento premio
Quando presenti l'annuncio, devi fornire un oggetto GADUserDidEarnRewardHandler per gestire il premio per l'utente.
Il seguente codice mostra il metodo migliore per visualizzare un annuncio interstitial con premio.
Swift
func showRewardedInterstitialAd() {
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
Ascolta gli eventi della UI nella visualizzazione per mostrare l'annuncio.
var rewardedInterstitialBody: some View {
// ...
}
.onChange(
of: showAd,
perform: { newValue in
if newValue {
viewModel.showAd()
}
}
)
Presenta l'annuncio interstitial con premio dal modello di visualizzazione:
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)showRewardedInterstitialAd {
[self.rewardedInterstitialAd presentFromRootViewController:self
userDidEarnRewardHandler:^{
GADAdReward *reward = self.rewardedInterstitialAd.adReward;
NSString *rewardMessage = [NSString
stringWithFormat:@"Reward received with "
@"currency %@ , amount %ld",
reward.type, [reward.amount longValue]];
NSLog(@"%@", rewardMessage);
// TODO: Reward the user.
}];
}
Esempi su GitHub
Visualizza gli esempi completi di annunci interstitial con premio nella tua lingua preferita:
Passaggi successivi
Scopri di più sulla privacy dell'utente.