Ön koşullar
Özel etkinlik ayarlarını tamamlayın.
Geçiş reklamı isteme
Özel etkinlik satır öğesine şelale uyumlulaştırma zincirinde ulaşıldığında,
loadInterstitial:adConfiguration:completionHandler:
yöntemi
özel oluştururken sağladığınız sınıf adı
bakın. Böyle durumlarda
bu yöntem SampleCustomEvent
içindedir ve bu durumda
loadInterstitial:adConfiguration:completionHandler:
yöntemi
SampleCustomEventInterstitial
.
Geçiş reklamı istemek için şunu uygulayan bir sınıf oluşturun veya değiştirin:
GADMediationAdapter
ve loadInterstitial:adConfiguration:completionHandler:
.
GADMediationAdapter
öğesini genişleten bir sınıf zaten varsa şunu uygula:
loadInterstitial:adConfiguration:completionHandler:
burada. Ayrıca,
GADMediationInterstitialAd
uygulamak için yeni bir sınıf oluşturun.
Özel etkinlik örneğimizde,
SampleCustomEvent
uygular
GADMediationAdapter
arayüzünü kullanır ve ardından
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
şu görevlerden sorumludur:
Geçiş reklamını yükleme ve
GADMediationInterstitialAdLoadCompletionHandler
yöntemini kullanın.GADMediationInterstitialAd
protokolü uygulanıyor.Google Mobile Ads SDK'sına reklam etkinliği geri çağırma isteklerini alma ve bildirme.
Kullanıcı arayüzünde tanımlanan isteğe bağlı parametre:
dahil edilir.
Parametreye
adConfiguration.credentials.settings[@"parameter"]
Bu parametre genellikle bir reklam ağı SDK'sının reklam nesnesi oluştururken ihtiyaç duyduğu bir reklam birimi tanımlayıcısıdır.
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]; }
Reklam başarılı bir şekilde getirildiğinde veya bir hatayla karşılaşıldığında
GADMediationInterstitialLoadCompletionHandler
araması yapar. Başarılı olursa hata parametresi için nil
değeriyle GADMediationInterstitialAd
uygulayan sınıfı iletin; başarısız olursa karşılaştığınız hatayı iletin.
Genellikle bu yöntemler
üçüncü taraf SDK'sı oluşturun. Bu örnekte, örnek SDK
alakalı geri çağırmaları içeren bir SampleInterstitialAdDelegate
içeriyor:
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
, görüntüleme için present
yönteminin uygulanmasını gerektiriyor
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] } }
Uyumlulaştırma etkinliklerini Google Mobile Ads SDK'sına yönlendirme
Yüklenmiş bir şekilde GADMediationInterstitialLoadCompletionHandler
adlı kişiyi çağırdıktan sonra
geri dönerse, döndürülen GADMediationInterstitialAdEventDelegate
yetki nesnesi
Ardından, bağdaştırıcı tarafından üçüncü taraftan sunu etkinliklerini yönlendirmek için kullanılır.
Google Mobile Ads SDK'sına entegre edin. SampleCustomEventInterstitial
sınıfı
şuradan geri çağırmaları yönlendirmek için SampleInterstitialAdDelegate
protokolünü uygular:
örnek reklam ağını Google Mobile Ads SDK'sına aktarın.
Özel etkinliğinizin bu geri çağırmalardan mümkün olduğunca çoğunu yönlendirmesi Böylece uygulamanız, Google Ads'den bu eşdeğer etkinlikleri Mobile Ads SDK'sı. Aşağıda, geri çağırma işlevinin kullanımına dair bir örnek verilmiştir:
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]; }
Böylece geçiş reklamları için özel etkinlik uygulaması tamamlanır. Örneğin tamamını GitHub'da bulabilirsiniz. Bunu zaten desteklenen bir reklam ağıyla kullanabilir veya şuna göre değiştirebilirsiniz: Görüntülü reklam özel etkinlik geçiş reklamları.