เหตุการณ์ที่กำหนดเองของโฆษณาแบนเนอร์
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ข้อกำหนดเบื้องต้น
ทําการตั้งค่าเหตุการณ์ที่กําหนดเองให้เสร็จสมบูรณ์
ขอโฆษณาแบนเนอร์
เมื่อถึงรายการโฆษณาเหตุการณ์ที่กําหนดเองในห่วงโซ่สื่อกลาง Waterfall ระบบจะเรียกใช้เมธอด loadBanner:adConfiguration:completionHandler:
ในชื่อคลาสที่คุณระบุเมื่อสร้างเหตุการณ์ที่กําหนดเอง ในกรณีนี้ วิธีการดังกล่าวอยู่ใน SampleCustomEvent
ซึ่งจะเรียกใช้
วิธีการ loadBanner:adConfiguration:completionHandler:
ใน
SampleCustomEventBanner
หากต้องการขอโฆษณาแบนเนอร์ ให้สร้างหรือแก้ไขคลาสที่ใช้
GADMediationAdapter
และ loadBanner:adConfiguration:completionHandler:
หากมี
คลาสที่ขยาย GADMediationAdapter
อยู่แล้ว ให้ใช้
loadBanner:adConfiguration:completionHandler:
ในคลาสนั้น นอกจากนี้ ให้สร้างชั้นเรียนใหม่เพื่อใช้ GADMediationBannerAd
ในตัวอย่างเหตุการณ์ที่กําหนดเอง
SampleCustomEvent
จะใช้
อินเทอร์เฟซ GADMediationAdapter
แล้วส่งต่อให้
SampleCustomEventBanner
Swift
import GoogleMobileAds
class SampleCustomEvent: NSObject, MediationAdapter {
fileprivate var bannerAd: SampleCustomEventBanner?
...
func loadBanner(
for adConfiguration: MediationBannerAdConfiguration,
completionHandler: @escaping GADMediationBannerLoadCompletionHandler
) {
self.bannerAd = SampleCustomEventBanner()
self.bannerAd?.loadBanner(
for: adConfiguration, completionHandler: completionHandler)
}
}
Objective-C
#import "SampleCustomEvent.h"
@implementation SampleCustomEvent
...
SampleCustomEventBanner *sampleBanner;
- (void)loadBannerForAdConfiguration:
(GADMediationBannerAdConfiguration *)adConfiguration
completionHandler:(GADMediationBannerLoadCompletionHandler)
completionHandler {
sampleBanner = [[SampleCustomEventBanner alloc] init];
[sampleBanner loadBannerForAdConfiguration:adConfiguration
completionHandler:completionHandler];
}
SampleCustomEventBanner
มีหน้าที่รับผิดชอบงานต่อไปนี้
โหลดโฆษณาแบนเนอร์และเรียกใช้เมธอด GADMediationBannerLoadCompletionHandler
เมื่อโหลดเสร็จสมบูรณ์
การติดตั้งใช้งานโปรโตคอล GADMediationBannerAd
รับและรายงานการเรียกกลับของเหตุการณ์โฆษณาไปยัง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google
พารามิเตอร์ที่ไม่บังคับซึ่งกำหนดไว้ใน UI ของ AdMob จะรวมอยู่ในการกำหนดค่าโฆษณา
เข้าถึงพารามิเตอร์ได้ผ่าน
adConfiguration.credentials.settings[@"parameter"]
โดยปกติพารามิเตอร์นี้คือตัวระบุหน่วยโฆษณาที่ SDK เครือข่ายโฆษณาต้องการเมื่อสร้างออบเจ็กต์โฆษณา
Swift
class SampleCustomEventBanner: NSObject, MediationBannerAd {
/// The Sample Ad Network banner ad.
var bannerAd: SampleBanner?
/// The ad event delegate to forward ad rendering events to Google Mobile Ads SDK.
var delegate: MediationBannerAdEventDelegate?
/// Completion handler called after ad load
var completionHandler: GADMediationBannerLoadCompletionHandler?
func loadBanner(
for adConfiguration: MediationBannerAdConfiguration,
completionHandler: @escaping GADMediationBannerLoadCompletionHandler
) {
// Create the bannerView with the appropriate size.
let adSize = adConfiguration.adSize
bannerAd = SampleBanner(
frame: CGRect(x: 0, y: 0, width: adSize.size.width, height: adSize.size.height))
bannerAd?.delegate = self
bannerAd?.adUnit = adConfiguration.credentials.settings["parameter"] as? String
let adRequest = SampleAdRequest()
adRequest.testMode = adConfiguration.isTestRequest
self.completionHandler = completionHandler
bannerAd?.fetchAd(adRequest)
}
}
Objective-C
#import "SampleCustomEventBanner.h"
@interface SampleCustomEventBanner () <SampleBannerAdDelegate,
GADMediationBannerAd> {
/// The sample banner ad.
SampleBanner *_bannerAd;
/// The completion handler to call when the ad loading succeeds or fails.
GADMediationBannerLoadCompletionHandler _loadCompletionHandler;
/// The ad event delegate to forward ad rendering events to the Google Mobile
/// Ads SDK.
id <GADMediationBannerAdEventDelegate> _adEventDelegate;
}
@end
@implementation SampleCustomEventBanner
- (void)loadBannerForAdConfiguration:
(GADMediationBannerAdConfiguration *)adConfiguration
completionHandler:(GADMediationBannerLoadCompletionHandler)
completionHandler {
__block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT;
__block GADMediationBannerLoadCompletionHandler originalCompletionHandler =
[completionHandler copy];
_loadCompletionHandler = ^id<GADMediationBannerAdEventDelegate>(
_Nullable id<GADMediationBannerAd> ad, NSError *_Nullable error) {
// Only allow completion handler to be called once.
if (atomic_flag_test_and_set(&completionHandlerCalled)) {
return nil;
}
id<GADMediationBannerAdEventDelegate> 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"];
_bannerAd = [[SampleBanner alloc]
initWithFrame:CGRectMake(0, 0, adConfiguration.adSize.size.width,
adConfiguration.adSize.size.height)];
_bannerAd.adUnit = adUnit;
_bannerAd.delegate = self;
SampleAdRequest *adRequest = [[SampleAdRequest alloc] init];
adRequest.testMode = adConfiguration.isTestRequest;
[_bannerAd fetchAd:adRequest];
}
ไม่ว่าระบบจะดึงข้อมูลโฆษณาสำเร็จหรือพบข้อผิดพลาด คุณ
จะเรียกใช้ GADMediationBannerLoadCompletionHandler
ในกรณีที่สำเร็จ ให้ส่งผ่านคลาสที่ใช้ GADMediationBannerAd
โดยมีค่า nil
สําหรับพารามิเตอร์ข้อผิดพลาด ในกรณีที่ล้มเหลว ให้ส่งผ่านข้อผิดพลาดที่คุณ
พบ
โดยปกติแล้ว วิธีการเหล่านี้จะได้รับการติดตั้งใช้งานภายในโค้ดเรียกกลับจาก SDK ของบุคคลที่สามที่อะแดปเตอร์ของคุณติดตั้งใช้งาน ในตัวอย่างนี้ SDK ตัวอย่าง
มี SampleBannerAdDelegate
พร้อม Callback ที่เกี่ยวข้องดังนี้
Swift
func bannerDidLoad(_ banner: SampleBanner) {
if let handler = completionHandler {
delegate = handler(self, nil)
}
}
func banner(
_ banner: SampleBanner, 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)bannerDidLoad:(SampleBanner *)banner {
_adEventDelegate = _loadCompletionHandler(self, nil);
}
- (void)banner:(SampleBanner *)banner
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);
}
GADMediationBannerAd
ต้องใช้พร็อพเพอร์ตี้ UIView
ดังนี้
Swift
var view: UIView {
return bannerAd ?? UIView()
}
Objective-C
- (nonnull UIView *)view {
return _bannerAd;
}
เมื่อเรียกใช้ GADMediationBannerLoadCompletionHandler
พร้อมโฆษณาที่โหลดแล้ว
ออบเจ็กต์ผู้มอบสิทธิ์ GADMediationBannerAdEventDelegate
ที่ส่งกลับมาจะใช้
โดยอะแดปเตอร์เพื่อส่งต่อเหตุการณ์การนำเสนอจาก SDK ของบุคคลที่สามไปยัง
SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google ได้ คลาส SampleCustomEventBanner
จะใช้โปรโตคอล
SampleBannerAdDelegate
เพื่อส่งต่อการเรียกกลับจากเครือข่ายโฆษณาตัวอย่างไปยัง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google
คุณควรส่งต่อการเรียกกลับเหล่านี้ให้มากที่สุดเท่าที่จะเป็นไปได้สำหรับเหตุการณ์ที่กำหนดเอง เพื่อให้แอปได้รับเหตุการณ์ที่เทียบเท่าจาก Google Mobile Ads SDK
ตัวอย่างการใช้ฟังก์ชันเรียกกลับ
Swift
func bannerWillLeaveApplication(_ banner: SampleBanner) {
delegate?.reportClick()
}
Objective-C
- (void)bannerWillLeaveApplication:(SampleBanner *)banner {
[_adEventDelegate reportClick];
}
ซึ่งจะทำให้การติดตั้งใช้งานเหตุการณ์ที่กำหนดเองสำหรับโฆษณาแบนเนอร์เสร็จสมบูรณ์ ดูตัวอย่างฉบับเต็มได้ที่
GitHub
คุณสามารถใช้กับเครือข่ายโฆษณาที่รองรับอยู่แล้ว หรือแก้ไขเพื่อแสดงโฆษณาแบนเนอร์เหตุการณ์ที่กำหนดเองได้
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-09-05 UTC
[null,null,["อัปเดตล่าสุด 2025-09-05 UTC"],[[["\u003cp\u003eTo request a banner ad using custom events, create a class that implements \u003ccode\u003eGADMediationAdapter\u003c/code\u003e and \u003ccode\u003eloadBanner:adConfiguration:completionHandler:\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eSampleCustomEventBanner\u003c/code\u003e is responsible for loading banner ads, implementing the \u003ccode\u003eGADMediationBannerAd\u003c/code\u003e protocol, and reporting ad events to the Google Mobile Ads SDK.\u003c/p\u003e\n"],["\u003cp\u003eAdMob UI parameters can be accessed in your code using \u003ccode\u003eadConfiguration.credentials.settings[@"parameter"]\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eYou need to forward mediation events from the third-party SDK to the Google Mobile Ads SDK by using \u003ccode\u003eGADMediationBannerAdEventDelegate\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eA full example implementation of banner custom events is available on GitHub for reference or modification.\u003c/p\u003e\n"]]],["To load a banner ad using custom events, implement `GADMediationAdapter` and `loadBanner:adConfiguration:completionHandler:` in a designated class (e.g., `SampleCustomEvent`). This class then delegates to another, which implements `GADMediationBannerAd` (e.g., `SampleCustomEventBanner`). `SampleCustomEventBanner` loads the ad, calls `GADMediationBannerLoadCompletionHandler` upon completion (either success or failure), implements `UIView` property, and forwards ad event callbacks from the third-party SDK to the Google Mobile Ads SDK via `GADMediationBannerAdEventDelegate`. The ad unit identifier is passed through `adConfiguration.credentials.settings`.\n"],null,["Prerequisites\n\nComplete the [custom events setup](/admob/ios/custom-events/setup).\n\nRequest a banner ad\n\nWhen the custom event line item is reached in the waterfall mediation chain,\nthe `loadBanner:adConfiguration:completionHandler:` method is called on the\nclass name you provided when [creating a custom\nevent](/admob/ios/custom-events/setup#create). In this case,\nthat method is in `SampleCustomEvent`, which then calls\nthe `loadBanner:adConfiguration:completionHandler:` method in\n`SampleCustomEventBanner`.\n\nTo request a banner ad, create or modify a class that implements\n`GADMediationAdapter` and `loadBanner:adConfiguration:completionHandler:`. If a\nclass that extends `GADMediationAdapter` already exists, implement\n`loadBanner:adConfiguration:completionHandler:` there. Additionally, create a\nnew class to implement `GADMediationBannerAd`.\n\nIn our custom event example,\n[`SampleCustomEvent`](//github.com/googleads/googleads-mobile-ios-mediation/blob/main/example/CustomEvent/SampleCustomEvent.m) implements\nthe `GADMediationAdapter` interface and then delegates to\n`SampleCustomEventBanner`. \n\nSwift \n\n```swift\nimport GoogleMobileAds\n\nclass SampleCustomEvent: NSObject, MediationAdapter {\n\n fileprivate var bannerAd: SampleCustomEventBanner?\n ...\n\n func loadBanner(\n for adConfiguration: MediationBannerAdConfiguration,\n completionHandler: @escaping GADMediationBannerLoadCompletionHandler\n ) {\n self.bannerAd = SampleCustomEventBanner()\n self.bannerAd?.loadBanner(\n for: adConfiguration, completionHandler: completionHandler)\n }\n}\n```\n\nObjective-C \n\n```objective-c\n#import \"SampleCustomEvent.h\"\n\n@implementation SampleCustomEvent\n...\n\nSampleCustomEventBanner *sampleBanner;\n\n- (void)loadBannerForAdConfiguration:\n (GADMediationBannerAdConfiguration *)adConfiguration\n completionHandler:(GADMediationBannerLoadCompletionHandler)\n completionHandler {\n sampleBanner = [[SampleCustomEventBanner alloc] init];\n [sampleBanner loadBannerForAdConfiguration:adConfiguration\n completionHandler:completionHandler];\n}\n```\n\n`SampleCustomEventBanner` is responsible for the following tasks:\n\n- Loading the banner ad and invoking a\n [`GADMediationBannerLoadCompletionHandler`](/admob/ios/api/reference/Protocols/GADMediationAdapter) method once\n loading completes.\n\n- Implementing the `GADMediationBannerAd` protocol.\n\n- Receiving and reporting ad event callbacks to Google Mobile Ads SDK.\n\nThe optional parameter defined in the AdMob UI is\nincluded in the ad configuration.\nThe parameter can be accessed through\n`adConfiguration.credentials.settings[@\"parameter\"]`. This parameter is\ntypically an ad unit identifier that an ad network SDK requires when\ninstantiating an ad object. \n\nSwift \n\n```swift\nclass SampleCustomEventBanner: NSObject, MediationBannerAd {\n /// The Sample Ad Network banner ad.\n var bannerAd: SampleBanner?\n\n /// The ad event delegate to forward ad rendering events to Google Mobile Ads SDK.\n var delegate: MediationBannerAdEventDelegate?\n\n /// Completion handler called after ad load\n var completionHandler: GADMediationBannerLoadCompletionHandler?\n\n func loadBanner(\n for adConfiguration: MediationBannerAdConfiguration,\n completionHandler: @escaping GADMediationBannerLoadCompletionHandler\n ) {\n // Create the bannerView with the appropriate size.\n let adSize = adConfiguration.adSize\n bannerAd = SampleBanner(\n frame: CGRect(x: 0, y: 0, width: adSize.size.width, height: adSize.size.height))\n bannerAd?.delegate = self\n bannerAd?.adUnit = adConfiguration.credentials.settings[\"parameter\"] as? String\n let adRequest = SampleAdRequest()\n adRequest.testMode = adConfiguration.isTestRequest\n self.completionHandler = completionHandler\n bannerAd?.fetchAd(adRequest)\n }\n}\n```\n\nObjective-C \n\n```objective-c\n#import \"SampleCustomEventBanner.h\"\n\n@interface SampleCustomEventBanner () \u003cSampleBannerAdDelegate,\n GADMediationBannerAd\u003e {\n /// The sample banner ad.\n SampleBanner *_bannerAd;\n\n /// The completion handler to call when the ad loading succeeds or fails.\n GADMediationBannerLoadCompletionHandler _loadCompletionHandler;\n\n /// The ad event delegate to forward ad rendering events to the Google Mobile\n /// Ads SDK.\n id \u003cGADMediationBannerAdEventDelegate\u003e _adEventDelegate;\n}\n@end\n\n@implementation SampleCustomEventBanner\n\n- (void)loadBannerForAdConfiguration:\n (GADMediationBannerAdConfiguration *)adConfiguration\n completionHandler:(GADMediationBannerLoadCompletionHandler)\n completionHandler {\n __block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT;\n __block GADMediationBannerLoadCompletionHandler originalCompletionHandler =\n [completionHandler copy];\n\n _loadCompletionHandler = ^id\u003cGADMediationBannerAdEventDelegate\u003e(\n _Nullable id\u003cGADMediationBannerAd\u003e ad, NSError *_Nullable error) {\n // Only allow completion handler to be called once.\n if (atomic_flag_test_and_set(&completionHandlerCalled)) {\n return nil;\n }\n\n id\u003cGADMediationBannerAdEventDelegate\u003e delegate = nil;\n if (originalCompletionHandler) {\n // Call original handler and hold on to its return value.\n delegate = originalCompletionHandler(ad, error);\n }\n\n // Release reference to handler. Objects retained by the handler will also\n // be released.\n originalCompletionHandler = nil;\n\n return delegate;\n };\n\n NSString *adUnit = adConfiguration.credentials.settings[@\"parameter\"];\n _bannerAd = [[SampleBanner alloc]\n initWithFrame:CGRectMake(0, 0, adConfiguration.adSize.size.width,\n adConfiguration.adSize.size.height)];\n _bannerAd.adUnit = adUnit;\n _bannerAd.delegate = self;\n SampleAdRequest *adRequest = [[SampleAdRequest alloc] init];\n adRequest.testMode = adConfiguration.isTestRequest;\n [_bannerAd fetchAd:adRequest];\n}\n```\n\nWhether the ad is successfully fetched or encounters an error, you\nwould call `GADMediationBannerLoadCompletionHandler`. In the event of success,\npass through the class that implements `GADMediationBannerAd` with a `nil` value\nfor the error parameter; in the event of failure, pass through the error you\nencountered.\n\nTypically, these methods are implemented inside callbacks from the\nthird-party SDK your adapter implements. For this example, the Sample SDK\nhas a `SampleBannerAdDelegate` with relevant callbacks: \n\nSwift \n\n```swift\nfunc bannerDidLoad(_ banner: SampleBanner) {\n if let handler = completionHandler {\n delegate = handler(self, nil)\n }\n}\n\nfunc banner(\n _ banner: SampleBanner, didFailToLoadAdWith errorCode: SampleErrorCode\n) {\n let error =\n SampleCustomEventUtilsSwift.SampleCustomEventErrorWithCodeAndDescription(\n code: SampleCustomEventErrorCodeSwift\n .SampleCustomEventErrorAdLoadFailureCallback,\n description:\n \"Sample SDK returned an ad load failure callback with error code: \\(errorCode)\"\n )\n if let handler = completionHandler {\n delegate = handler(nil, error)\n }\n}\n```\n\nObjective-C \n\n```objective-c\n- (void)bannerDidLoad:(SampleBanner *)banner {\n _adEventDelegate = _loadCompletionHandler(self, nil);\n}\n\n- (void)banner:(SampleBanner *)banner\n didFailToLoadAdWithErrorCode:(SampleErrorCode)errorCode {\n NSError *error = SampleCustomEventErrorWithCodeAndDescription(\n SampleCustomEventErrorAdLoadFailureCallback,\n [NSString stringWithFormat:@\"Sample SDK returned an ad load failure \"\n @\"callback with error code: %@\",\n errorCode]);\n _adEventDelegate = _loadCompletionHandler(nil, error);\n}\n```\n\n`GADMediationBannerAd` requires implementing a `UIView` property: \n\nSwift \n\n```swift\nvar view: UIView {\n return bannerAd ?? UIView()\n}\n```\n\nObjective-C \n\n```objective-c\n- (nonnull UIView *)view {\n return _bannerAd;\n}\n```\n\nForward mediation events to Google Mobile Ads SDK\n\nOnce you've called `GADMediationBannerLoadCompletionHandler` with a loaded ad,\nthe returned `GADMediationBannerAdEventDelegate` delegate object can then be\nused by the adapter to forward presentation events from the third-party SDK to\nGoogle Mobile Ads SDK. The `SampleCustomEventBanner` class implements the\n`SampleBannerAdDelegate` protocol to forward callbacks from the sample ad\nnetwork to Google Mobile Ads SDK.\n\nIt's important that your custom event forwards as many of these callbacks as\npossible, so that your app receives these equivalent events from Google Mobile Ads SDK.\nHere's an example of using callbacks: \n\nSwift \n\n```swift\nfunc bannerWillLeaveApplication(_ banner: SampleBanner) {\n delegate?.reportClick()\n}\n```\n\nObjective-C \n\n```objective-c\n- (void)bannerWillLeaveApplication:(SampleBanner *)banner {\n [_adEventDelegate reportClick];\n}\n```\n\nThis completes the custom events implementation for banner ads. The full example\nis available on\n[GitHub](//github.com/googleads/googleads-mobile-ios-mediation/tree/main/example/CustomEvent).\nYou can use it with an ad network that is already supported or modify it to\ndisplay custom event banner ads."]]