Wymagania wstępne
Dokończ konfigurowanie zdarzeń niestandardowych.
Wysyłanie żądania reklamy pełnoekranowej
Gdy element zamówienia zdarzenia niestandardowego zostanie osiągnięty w łańcuchu zapośredniczenia kaskadowego,
metoda loadInterstitial:adConfiguration:completionHandler:
jest wywoływana
nazwa klasy podana podczas tworzenia niestandardowej
. W tym przypadku
ta metoda jest w komórce SampleCustomEvent
, która następnie wywołuje
metody loadInterstitial:adConfiguration:completionHandler:
w
SampleCustomEventInterstitial
Aby zażądać reklamy pełnoekranowej, utwórz lub zmodyfikuj klasę, która implementuje
GADMediationAdapter
i loadInterstitial:adConfiguration:completionHandler:
.
Jeśli klasa rozszerzająca GADMediationAdapter
już istnieje, zaimplementuj w niej funkcję loadInterstitial:adConfiguration:completionHandler:
. Dodatkowo utwórz nową klasę, aby zaimplementować GADMediationInterstitialAd
.
W naszym przykładzie zdarzenia niestandardowego funkcja SampleCustomEvent
implementuje interfejs GADMediationAdapter
, a potem przekazuje sterowanie do funkcji SampleCustomEventInterstitial
.
Swift
import GoogleMobileAds class SampleCustomEvent: NSObject, GADMediationAdapter { fileprivate var interstitialAd: SampleCustomEventInterstitial? ... func loadInterstitial( for adConfiguration: GADMediationInterstitialAdConfiguration, completionHandler: @escaping GADMediationInterstitialLoadCompletionHandler ) { self.interstitialAd = SampleCustomEventInterstitial() self.interstitialAd?.loadInterstitial( for: adConfiguration, completionHandler: completionHandler) } }
Objective-C
#import "SampleCustomEvent.h" @implementation SampleCustomEvent SampleCustomEventInterstitial *sampleInterstitial; - (void)loadInterstitialForAdConfiguration: (GADMediationInterstitialAdConfiguration *)adConfiguration completionHandler: (GADMediationInterstitialLoadCompletionHandler) completionHandler { sampleInterstitial = [[SampleCustomEventInterstitial alloc] init]; [sampleInterstitial loadInterstitialForAdConfiguration:adConfiguration completionHandler:completionHandler]; }
SampleCustomEventInterstitial
odpowiada za następujące zadania:
Wczytanie reklamy pełnoekranowej i wywołanie
GADMediationInterstitialAdLoadCompletionHandler
po zakończeniu wczytywania.Zaimplementuj protokół
GADMediationInterstitialAd
.Odbieranie i zgłaszanie do pakietu SDK do reklam mobilnych Google wywołań zwrotnych zdarzeń reklamy.
Opcjonalny parametr zdefiniowany w interfejsie to
uwzględnionych w konfiguracji reklamy.
Dostęp do parametru można uzyskać za pomocą pliku adConfiguration.credentials.settings[@"parameter"]
. Ten parametr to zwykle identyfikator jednostki reklamowej, którego pakiet SDK sieci reklamowej wymaga do utworzenia obiektu reklamy.
Swift
import GoogleMobileAds class SampleCustomEventInterstitial: NSObject, GADMediationInterstitialAd { /// The Sample Ad Network interstitial ad. var interstitial: SampleInterstitial? /// The ad event delegate to forward ad rendering events to the Google Mobile Ads SDK. var delegate: GADMediationInterstitialAdEventDelegate? var completionHandler: GADMediationInterstitialLoadCompletionHandler? func loadInterstitial( for adConfiguration: GADMediationInterstitialAdConfiguration, completionHandler: @escaping GADMediationInterstitialLoadCompletionHandler ) { interstitial = SampleInterstitial.init( adUnitID: adConfiguration.credentials.settings["parameter"] as? String) interstitial?.delegate = self let adRequest = SampleAdRequest() adRequest.testMode = adConfiguration.isTestRequest self.completionHandler = completionHandler interstitial?.fetchAd(adRequest) } func present(from viewController: UIViewController) { if let interstitial = interstitial, interstitial.isInterstitialLoaded { interstitial.show() } } }
Objective-C
#import "SampleCustomEventInterstitial.h" @interface SampleCustomEventInterstitial () <SampleInterstitialAdDelegate, GADMediationInterstitialAd> { /// The sample interstitial ad. SampleInterstitial *_interstitialAd; /// The completion handler to call when the ad loading succeeds or fails. GADMediationInterstitialLoadCompletionHandler _loadCompletionHandler; /// The ad event delegate to forward ad rendering events to the Google Mobile /// Ads SDK. id <GADMediationInterstitialAdEventDelegate> _adEventDelegate; } @end - (void)loadInterstitialForAdConfiguration: (GADMediationInterstitialAdConfiguration *)adConfiguration completionHandler: (GADMediationInterstitialLoadCompletionHandler) completionHandler { __block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT; __block GADMediationInterstitialLoadCompletionHandler originalCompletionHandler = [completionHandler copy]; _loadCompletionHandler = ^id<GADMediationInterstitialAdEventDelegate>( _Nullable id<GADMediationInterstitialAd> ad, NSError *_Nullable error) { // Only allow completion handler to be called once. if (atomic_flag_test_and_set(&completionHandlerCalled)) { return nil; } id<GADMediationInterstitialAdEventDelegate> delegate = nil; if (originalCompletionHandler) { // Call original handler and hold on to its return value. delegate = originalCompletionHandler(ad, error); } // Release reference to handler. Objects retained by the handler will also // be released. originalCompletionHandler = nil; return delegate; }; NSString *adUnit = adConfiguration.credentials.settings[@"parameter"]; _interstitialAd = [[SampleInterstitial alloc] initWithAdUnitID:adUnit]; _interstitialAd.delegate = self; SampleAdRequest *adRequest = [[SampleAdRequest alloc] init]; adRequest.testMode = adConfiguration.isTestRequest; [_interstitialAd fetchAd:adRequest]; }
Bez względu na to, czy pobieranie reklamy się powiedzie, czy wystąpi błąd, wywołasz funkcję GADMediationInterstitialLoadCompletionHandler
. W przypadku powodzenia prześlij klasę implementującą GADMediationInterstitialAd
z wartością nil
dla parametru błędu. W przypadku niepowodzenia prześlij wygenerowany błąd.
Te metody są zwykle implementowane wewnątrz wywołań zwrotnych z funkcji
pakietu SDK innej firmy wdrożonego przez adapter. W tym przykładzie Przykładowy pakiet SDK
zawiera SampleInterstitialAdDelegate
z odpowiednimi wywołaniami zwrotnymi:
Swift
func interstitialDidLoad(_ interstitial: SampleInterstitial) { if let handler = completionHandler { delegate = handler(self, nil) } } func interstitial( _ interstitial: SampleInterstitial, didFailToLoadAdWith errorCode: SampleErrorCode ) { let error = SampleCustomEventUtilsSwift.SampleCustomEventErrorWithCodeAndDescription( code: SampleCustomEventErrorCodeSwift .SampleCustomEventErrorAdLoadFailureCallback, description: "Sample SDK returned an ad load failure callback with error code: \(errorCode)" ) if let handler = completionHandler { delegate = handler(nil, error) } }
Objective-C
- (void)interstitialDidLoad:(SampleInterstitial *)interstitial { _adEventDelegate = _loadCompletionHandler(self, nil); } - (void)interstitial:(SampleInterstitial *)interstitial didFailToLoadAdWithErrorCode:(SampleErrorCode)errorCode { NSError *error = SampleCustomEventErrorWithCodeAndDescription( SampleCustomEventErrorAdLoadFailureCallback, [NSString stringWithFormat:@"Sample SDK returned an ad load failure " @"callback with error code: %@", errorCode]); _adEventDelegate = _loadCompletionHandler(nil, error); }
GADMediationInterstitialAd
wymaga zaimplementowania metody present
, aby wyświetlić reklamę:
Swift
func present(from viewController: UIViewController) { if let interstitial = interstitial, interstitial.isInterstitialLoaded { interstitial.show() } }
Objective-C
- (void)presentFromViewController:(UIViewController *)viewController { if ([_interstitialAd isInterstitialLoaded]) { [_interstitialAd show]; } else { NSError *error = SampleCustomEventErrorWithCodeAndDescription( SampleCustomEventErrorAdNotLoaded, [NSString stringWithFormat:@"The interstitial ad failed to present " @"because the ad was not loaded."]); [_adEventDelegate didFailToPresentWithError:error] } }
Przekazywanie zdarzeń zapośredniczenia do pakietu SDK do reklam mobilnych Google
Po wywołaniu metody GADMediationInterstitialLoadCompletionHandler
z załadowaną reklamą adapter może użyć zwróconego obiektu GADMediationInterstitialAdEventDelegate
, aby przekazywać zdarzenia związane z prezentacją z zewnętrznego pakietu SDK do pakietu SDK do reklam mobilnych Google. Klasa SampleCustomEventInterstitial
implementuje protokół SampleInterstitialAdDelegate
, aby przekazywać wywołania zwrotne z próbnej sieci reklamowej do pakietu SDK do reklam mobilnych Google.
Ważne jest, aby Twoje zdarzenie niestandardowe przekazywało jak najwięcej wywołań zwrotnych, aby aplikacja otrzymywała te zdarzenia od pakietu SDK do reklam mobilnych Google. Oto przykład użycia wywołań zwrotnych:
Swift
func interstitialWillPresentScreen(_ interstitial: SampleInterstitial) { delegate?.willPresentFullScreenView() delegate?.reportImpression() } func interstitialWillDismissScreen(_ interstitial: SampleInterstitial) { delegate?.willDismissFullScreenView() } func interstitialDidDismissScreen(_ interstitial: SampleInterstitial) { delegate?.didDismissFullScreenView() } func interstitialWillLeaveApplication(_ interstitial: SampleInterstitial) { delegate?.reportClick() }
Objective-C
- (void)interstitialWillPresentScreen:(SampleInterstitial *)interstitial { [_adEventDelegate willPresentFullScreenView]; [_adEventDelegate reportImpression]; } - (void)interstitialWillDismissScreen:(SampleInterstitial *)interstitial { [_adEventDelegate willDismissFullScreenView]; } - (void)interstitialDidDismissScreen:(SampleInterstitial *)interstitial { [_adEventDelegate didDismissFullScreenView]; } - (void)interstitialWillLeaveApplication:(SampleInterstitial *)interstitial { [_adEventDelegate reportClick]; }
To kończy implementację zdarzeń niestandardowych w przypadku reklam pełnoekranowych. Pełny przykład jest dostępny na GitHubie. Możesz go używać w sieci reklamowej, która jest już obsługiwana, lub zmienić ją na wyświetlać pełnoekranowe reklamy zdarzeń niestandardowych.