เหตุการณ์ที่กำหนดเองของโฆษณาเนทีฟ

ข้อกำหนดเบื้องต้น

ทําการตั้งค่าเหตุการณ์ที่กําหนดเองให้เสร็จสมบูรณ์

ขอโฆษณาเนทีฟ

เมื่อถึงรายการโฆษณาเหตุการณ์ที่กําหนดเองในห่วงโซ่สื่อกลาง Waterfall ระบบจะเรียกใช้เมธอด loadNativeAd:adConfiguration:completionHandler: ในชื่อคลาสที่คุณระบุเมื่อสร้างเหตุการณ์ที่กําหนดเอง ในกรณีนี้ วิธีการดังกล่าวอยู่ใน SampleCustomEvent ซึ่งจะเรียกใช้ วิธีการ loadNativeAd:adConfiguration:completionHandler: ใน SampleCustomEventNative

หากต้องการขอโฆษณาเนทีฟ ให้สร้างหรือแก้ไขคลาสที่ใช้ GADMediationAdapter และ loadNativeAd:adConfiguration:completionHandler: หากมีคลาสที่ขยาย GADMediationAdapter อยู่แล้ว ให้ใช้ loadNativeAd:adConfiguration:completionHandler: ในคลาสนั้น นอกจากนี้ ให้สร้าง ชั้นเรียนใหม่เพื่อใช้ GADMediationNativeAd

ในตัวอย่างเหตุการณ์ที่กําหนดเอง SampleCustomEvent จะใช้ อินเทอร์เฟซ GADMediationAdapter แล้วส่งต่อให้ SampleCustomEventNative

Swift

import GoogleMobileAds

class SampleCustomEvent: NSObject, MediationAdapter {

  fileprivate var nativeAd: SampleCustomEventNativeAd?

  func loadNativeAd(
    for adConfiguration: MediationNativeAdConfiguration,
    completionHandler: @escaping GADMediationNativeAdLoadCompletionHandler
  ) {
    self.nativeAd = SampleCustomEventNativeAd()
    self.nativeAd?.loadNativeAd(
      for: adConfiguration, completionHandler: completionHandler)
  }
}

Objective-C

#import "SampleCustomEvent.h"

@implementation SampleCustomEvent

SampleCustomEventNativeAd *sampleNativeAd;

- (void)loadNativeAdForAdConfiguration:
            (GADMediationNativeAdConfiguration *)adConfiguration
                     completionHandler:
                         (GADMediationNativeAdLoadCompletionHandler)
                             completionHandler {
  sampleNative = [[SampleCustomEventNativeAd alloc] init];
  [sampleNative loadNativeAdForAdConfiguration:adConfiguration
                             completionHandler:completionHandler];
}

`SampleCustomEventNative` มีหน้าที่รับผิดชอบต่อการดำเนินการต่อไปนี้

  • การโหลดโฆษณาเนทีฟ

  • การติดตั้งใช้งานโปรโตคอล GADMediationNativeAd

  • รับและรายงานการเรียกกลับของเหตุการณ์โฆษณาไปยัง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google

พารามิเตอร์ที่ไม่บังคับที่กำหนดไว้ใน UI ของ Ad Manager จะรวมอยู่ในการกำหนดค่าโฆษณา เข้าถึงพารามิเตอร์ได้ผ่าน adConfiguration.credentials.settings[@"parameter"] โดยปกติพารามิเตอร์นี้คือตัวระบุหน่วยโฆษณาที่ SDK เครือข่ายโฆษณาต้องการเมื่อสร้างออบเจ็กต์โฆษณา

Swift

class SampleCustomEventNativeAd: NSObject, MediationNativeAd {
  /// The Sample Ad Network native ad.
  var nativeAd: SampleNativeAd?

  /// The ad event delegate to forward ad rendering events to the Google Mobile
  /// Ads SDK.
  var delegate: MediationNativeAdEventDelegate?

  /// Completion handler called after ad load
  var completionHandler: GADMediationNativeLoadCompletionHandler?

  func loadNativeAd(
    for adConfiguration: MediationNativeAdConfiguration,
    completionHandler: @escaping GADMediationNativeLoadCompletionHandler
  ) {
    let adLoader = SampleNativeAdLoader()
    let sampleRequest = SampleNativeAdRequest()

    // Google Mobile Ads SDK requires the image assets to be downloaded
    // automatically unless the publisher specifies otherwise by using the
    // GADNativeAdImageAdLoaderOptions object's disableImageLoading property. If
    // your network doesn't have an option like this and instead only ever
    // returns URLs for images (rather than the images themselves), your adapter
    // should download image assets on behalf of the publisher. This should be
    // done after receiving the native ad object from your network's SDK, and
    // before calling the connector's adapter:didReceiveMediatedNativeAd: method.
    sampleRequest.shouldDownloadImages = true
    sampleRequest.preferredImageOrientation = NativeAdImageOrientation.any
    sampleRequest.shouldRequestMultipleImages = false
    let options = adConfiguration.options
    for loaderOptions: AdLoaderOptions in options {
      if let imageOptions = loaderOptions as? NativeAdImageAdLoaderOptions {
        sampleRequest.shouldRequestMultipleImages =
          imageOptions.shouldRequestMultipleImages
        // If the GADNativeAdImageAdLoaderOptions' disableImageLoading property is
        // YES, the adapter should send just the URLs for the images.
        sampleRequest.shouldDownloadImages = !imageOptions.disableImageLoading
      } else if let mediaOptions = loaderOptions
        as? NativeAdMediaAdLoaderOptions
      {
        switch mediaOptions.mediaAspectRatio {
        case MediaAspectRatio.landscape:
          sampleRequest.preferredImageOrientation =
            NativeAdImageOrientation.landscape
        case MediaAspectRatio.portrait:
          sampleRequest.preferredImageOrientation =
            NativeAdImageOrientation.portrait
        default:
          sampleRequest.preferredImageOrientation = NativeAdImageOrientation.any
        }
      }
    }
    // This custom event uses the server parameter to carry an ad unit ID, which
    // is the most common use case.
    adLoader.delegate = self
    adLoader.adUnitID =
      adConfiguration.credentials.settings["parameter"] as? String
    self.completionHandler = completionHandler
    adLoader.fetchAd(sampleRequest)
  }
}

Objective-C

#import "SampleCustomEventNativeAd.h"

@interface SampleCustomEventNativeAd () <SampleNativeAdDelegate,
                                         GADMediationNativeAd> {
  /// The sample native ad.
  SampleNativeAd *_nativeAd;

  /// The completion handler to call when the ad loading succeeds or fails.
  GADMediationNativeLoadCompletionHandler _loadCompletionHandler;

  /// The ad event delegate to forward ad rendering events to the Google Mobile
  /// Ads SDK.
  id<GADMediationNativeAdEventDelegate> _adEventDelegate;
}
@end

- (void)loadNativeAdForAdConfiguration:
            (GADMediationNativeAdConfiguration *)adConfiguration
                     completionHandler:(GADMediationNativeLoadCompletionHandler)
                                           completionHandler {
  __block atomic_flag completionHandlerCalled = ATOMIC_FLAG_INIT;
  __block GADMediationNativeLoadCompletionHandler originalCompletionHandler =
      [completionHandler copy];

  _loadCompletionHandler = ^id<GADMediationNativeAdEventDelegate>(
      _Nullable id<GADMediationNativeAd> ad, NSError *_Nullable error) {
    // Only allow completion handler to be called once.
    if (atomic_flag_test_and_set(&completionHandlerCalled)) {
      return nil;
    }

    id<GADMediationNativeAdEventDelegate> 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;
  };

  SampleNativeAdLoader *adLoader = [[SampleNativeAdLoader alloc] init];
  SampleNativeAdRequest *sampleRequest = [[SampleNativeAdRequest alloc] init];

  // Google Mobile Ads SDK requires the image assets to be downloaded
  // automatically unless the publisher specifies otherwise by using the
  // GADNativeAdImageAdLoaderOptions object's disableImageLoading property. If
  // your network doesn't have an option like this and instead only ever returns
  // URLs for images (rather than the images themselves), your adapter should
  // download image assets on behalf of the publisher. This should be done after
  // receiving the native ad object from your network's SDK, and before calling
  // the connector's adapter:didReceiveMediatedNativeAd: method.
  sampleRequest.shouldDownloadImages = YES;

  sampleRequest.preferredImageOrientation = NativeAdImageOrientationAny;
  sampleRequest.shouldRequestMultipleImages = NO;
  sampleRequest.testMode = adConfiguration.isTestRequest;

  for (GADAdLoaderOptions *loaderOptions in adConfiguration.options) {
    if ([loaderOptions isKindOfClass:[GADNativeAdImageAdLoaderOptions class]]) {
      GADNativeAdImageAdLoaderOptions *imageOptions =
          (GADNativeAdImageAdLoaderOptions *)loaderOptions;
      sampleRequest.shouldRequestMultipleImages =
          imageOptions.shouldRequestMultipleImages;

      // If the GADNativeAdImageAdLoaderOptions' disableImageLoading property is
      // YES, the adapter should send just the URLs for the images.
      sampleRequest.shouldDownloadImages = !imageOptions.disableImageLoading;
    } else if ([loaderOptions
                   isKindOfClass:[GADNativeAdMediaAdLoaderOptions class]]) {
      GADNativeAdMediaAdLoaderOptions *mediaOptions =
          (GADNativeAdMediaAdLoaderOptions *)loaderOptions;
      switch (mediaOptions.mediaAspectRatio) {
        case GADMediaAspectRatioLandscape:
          sampleRequest.preferredImageOrientation =
              NativeAdImageOrientationLandscape;
          break;
        case GADMediaAspectRatioPortrait:
          sampleRequest.preferredImageOrientation =
              NativeAdImageOrientationPortrait;
          break;
        default:
          sampleRequest.preferredImageOrientation = NativeAdImageOrientationAny;
          break;
      }
    } else if ([loaderOptions isKindOfClass:[GADNativeAdViewAdOptions class]]) {
      _nativeAdViewAdOptions = (GADNativeAdViewAdOptions *)loaderOptions;
    }
  }

  // This custom event uses the server parameter to carry an ad unit ID, which
  // is the most common use case.
  NSString *adUnit = adConfiguration.credentials.settings[@"parameter"];
  adLoader.adUnitID = adUnit;
  adLoader.delegate = self;

  [adLoader fetchAd:sampleRequest];
}

ไม่ว่าระบบจะดึงข้อมูลโฆษณาสำเร็จหรือพบข้อผิดพลาด คุณ จะเรียกใช้ GADMediationNativeAdLoadCompletionHandler ในกรณีที่สำเร็จ ให้ส่งผ่านคลาสที่ใช้ GADMediationNativeAd โดยมีค่า nil สําหรับพารามิเตอร์ข้อผิดพลาด ในกรณีที่ล้มเหลว ให้ส่งผ่านข้อผิดพลาดที่คุณ พบ

โดยปกติแล้ว วิธีการเหล่านี้จะได้รับการติดตั้งใช้งานภายในโค้ดเรียกกลับจาก SDK ของบุคคลที่สามที่อะแดปเตอร์ของคุณติดตั้งใช้งาน ในตัวอย่างนี้ SDK ตัวอย่าง มี SampleNativeAdDelegate พร้อม Callback ที่เกี่ยวข้องดังนี้

Swift

func adLoader(
  _ adLoader: SampleNativeAdLoader, didReceive nativeAd: SampleNativeAd
) {
  extraAssets = [
    SampleCustomEventConstantsSwift.awesomenessKey: nativeAd.degreeOfAwesomeness
      ?? ""
  ]

  if let image = nativeAd.image {
    images = [NativeAdImage(image: image)]
  } else {
    let imageUrl = URL(fileURLWithPath: nativeAd.imageURL)
    images = [NativeAdImage(url: imageUrl, scale: nativeAd.imageScale)]
  }
  if let mappedIcon = nativeAd.icon {
    icon = NativeAdImage(image: mappedIcon)
  } else {
    let iconURL = URL(fileURLWithPath: nativeAd.iconURL)
    icon = NativeAdImage(url: iconURL, scale: nativeAd.iconScale)
  }

  adChoicesView = SampleAdInfoView()
  self.nativeAd = nativeAd
  if let handler = completionHandler {
    delegate = handler(self, nil)
  }
}

func adLoader(
  _ adLoader: SampleNativeAdLoader,
  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)adLoader:(SampleNativeAdLoader *)adLoader
    didReceiveNativeAd:(SampleNativeAd *)nativeAd {
  if (nativeAd.image) {
    _images = @[ [[GADNativeAdImage alloc] initWithImage:nativeAd.image] ];
  } else {
    NSURL *imageURL = [[NSURL alloc] initFileURLWithPath:nativeAd.imageURL];
    _images = @[ [[GADNativeAdImage alloc] initWithURL:imageURL
                                                 scale:nativeAd.imageScale] ];
  }

  if (nativeAd.icon) {
    _icon = [[GADNativeAdImage alloc] initWithImage:nativeAd.icon];
  } else {
    NSURL *iconURL = [[NSURL alloc] initFileURLWithPath:nativeAd.iconURL];
    _icon = [[GADNativeAdImage alloc] initWithURL:iconURL
                                            scale:nativeAd.iconScale];
  }

  // The sample SDK provides an AdChoices view (SampleAdInfoView). If your SDK
  // provides image and click through URLs for its AdChoices icon instead of an
  // actual UIView, the adapter is responsible for downloading the icon image
  // and creating the AdChoices icon view.
  _adChoicesView = [[SampleAdInfoView alloc] init];
  _nativeAd = nativeAd;

  _adEventDelegate = _loadCompletionHandler(self, nil);
}

- (void)adLoader:(SampleNativeAdLoader *)adLoader
    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);
}

โฆษณาเนทีฟใน Maps

SDK แต่ละรายการมีรูปแบบเฉพาะของโฆษณาเนทีฟ โดยอาจมีออบเจ็กต์ที่มีฟิลด์ "title" เป็นต้น ในขณะที่อีกออบเจ็กต์อาจมี "headline" นอกจากนี้ วิธีการที่ใช้ในการติดตามการแสดงผลและประมวลผล การคลิกอาจแตกต่างกันไปในแต่ละ SDK

หากต้องการแก้ไขปัญหาเหล่านี้ เมื่อเหตุการณ์ที่กำหนดเองได้รับออบเจ็กต์โฆษณาเนทีฟจาก SDK ที่ใช้สื่อกลาง เหตุการณ์ที่กำหนดเองควรใช้คลาสที่ใช้ GADMediationNativeAd เช่น SampleCustomEventNativeAd เพื่อ "แมป" ออบเจ็กต์โฆษณาเนทีฟของ SDK ที่ใช้สื่อกลาง เพื่อให้ตรงกับอินเทอร์เฟซที่ SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google คาดหวัง

ตอนนี้เรามาดูรายละเอียดการติดตั้งใช้งานสำหรับ SampleCustomEventNativeAdกัน

จัดเก็บการแมป

GADMediationNativeAd คาดว่าจะใช้พร็อพเพอร์ตี้บางอย่างที่ แมปจากพร็อพเพอร์ตี้ของ SDK อื่นๆ

Swift

var nativeAd: SampleNativeAd?

var headline: String? {
  return nativeAd?.headline
}

var images: [NativeAdImage]?

var body: String? {
  return nativeAd?.body
}

var icon: NativeAdImage?

var callToAction: String? {
  return nativeAd?.callToAction
}

var starRating: NSDecimalNumber? {
  return nativeAd?.starRating
}

var store: String? {
  return nativeAd?.store
}

var price: String? {
  return nativeAd?.price
}

var advertiser: String? {
  return nativeAd?.advertiser
}

var extraAssets: [String: Any]? {
  return [
    SampleCustomEventConstantsSwift.awesomenessKey:
      nativeAd?.degreeOfAwesomeness
      ?? ""
  ]
}

var adChoicesView: UIView?

var mediaView: UIView? {
  return nativeAd?.mediaView
}

Objective-C

/// Used to store the ad's images. In order to implement the
/// GADMediationNativeAd protocol, we use this class to return the images
/// property.
NSArray<GADNativeAdImage *> *_images;

/// Used to store the ad's icon. In order to implement the GADMediationNativeAd
/// protocol, we use this class to return the icon property.
GADNativeAdImage *_icon;

/// Used to store the ad's ad choices view. In order to implement the
/// GADMediationNativeAd protocol, we use this class to return the adChoicesView
/// property.
UIView *_adChoicesView;

- (nullable NSString *)headline {
  return _nativeAd.headline;
}

- (nullable NSArray<GADNativeAdImage *> *)images {
  return _images;
}

- (nullable NSString *)body {
  return _nativeAd.body;
}

- (nullable GADNativeAdImage *)icon {
  return _icon;
}

- (nullable NSString *)callToAction {
  return _nativeAd.callToAction;
}

- (nullable NSDecimalNumber *)starRating {
  return _nativeAd.starRating;
}

- (nullable NSString *)store {
  return _nativeAd.store;
}

- (nullable NSString *)price {
  return _nativeAd.price;
}

- (nullable NSString *)advertiser {
  return _nativeAd.advertiser;
}

- (nullable NSDictionary<NSString *, id> *)extraAssets {
  return
      @{SampleCustomEventExtraKeyAwesomeness : _nativeAd.degreeOfAwesomeness};
}

- (nullable UIView *)adChoicesView {
  return _adChoicesView;
}

- (nullable UIView *)mediaView {
  return _nativeAd.mediaView;
}

- (BOOL)hasVideoContent {
  return self.mediaView != nil;
}

เครือข่ายที่ใช้สื่อกลางบางเครือข่ายอาจมีชิ้นงานเพิ่มเติมนอกเหนือจากที่กำหนดโดย Google Mobile Ads SDK GADMediationNativeAd Protocol มีเมธอด ที่เรียกว่า extraAssets ซึ่ง Google Mobile Ads SDK ใช้เพื่อดึงข้อมูลชิ้นงาน "พิเศษ" เหล่านี้จากเครื่องมือแมป

ชิ้นงานรูปภาพแผนที่

การแมปชิ้นงานรูปภาพมีความซับซ้อนมากกว่าการแมปข้อมูลประเภทที่เรียบง่ายกว่า เช่น NSString หรือ double ระบบอาจดาวน์โหลดรูปภาพโดยอัตโนมัติหรือ แสดงเป็นค่า URL ความหนาแน่นของพิกเซลก็อาจแตกต่างกันไป

Google Mobile Ads SDK มีคลาส GADNativeAdImage เพื่อช่วยคุณจัดการรายละเอียดเหล่านี้ ข้อมูลชิ้นงานรูปภาพ (ไม่ว่าจะเป็นUIImage ออบเจ็กต์จริงหรือเป็นเพียงค่าNSURL) ควรส่งกลับไปยัง Google Mobile Ads SDK โดยใช้คลาสนี้

ต่อไปนี้คือวิธีที่คลาส Mapper จัดการการสร้าง GADNativeAdImage เพื่อเก็บรูปภาพไอคอน

Swift

if let image = nativeAd.image {
  images = [NativeAdImage(image: image)]
} else {
  let imageUrl = URL(fileURLWithPath: nativeAd.imageURL)
  images = [NativeAdImage(url: imageUrl, scale: nativeAd.imageScale)]
}

Objective-C

if (nativeAd.image) {
  _images = @[ [[GADNativeAdImage alloc] initWithImage:nativeAd.image] ];
} else {
  NSURL *imageURL = [[NSURL alloc] initFileURLWithPath:nativeAd.imageURL];
  _images = @[ [[GADNativeAdImage alloc] initWithURL:imageURL
                                               scale:nativeAd.imageScale] ];
}

เหตุการณ์การแสดงผลและการคลิก

ทั้ง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google และ SDK ที่ใช้สื่อกลางต้องทราบเมื่อเกิดการแสดงผลหรือการคลิก แต่มีเพียง SDK เดียวเท่านั้นที่ต้องติดตามเหตุการณ์เหล่านี้ เหตุการณ์ที่กําหนดเองใช้ได้ 2 วิธีที่แตกต่างกัน ขึ้นอยู่กับว่า SDK สื่อกลางรองรับการติดตามการแสดงผลและการคลิกด้วยตัวเองหรือไม่

ติดตามการคลิกและการแสดงผลด้วย SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google

หาก SDK ที่ใช้สื่อกลางไม่ได้ติดตามการแสดงผลและการคลิกของตัวเอง แต่ มีเมธอดสำหรับบันทึกการคลิกและการแสดงผล Google Mobile Ads SDK จะ ติดตามเหตุการณ์เหล่านี้และแจ้งอะแดปเตอร์ได้ โปรโตคอล GADMediationNativeAd มี 2 วิธี ได้แก่ didRecordImpression: และ didRecordClickOnAssetWithName:view:viewController: ซึ่งเหตุการณ์ที่กำหนดเองสามารถ ใช้เพื่อเรียกเมธอดที่เกี่ยวข้องในออบเจ็กต์โฆษณาเนทีฟที่สื่อกลางได้

Swift

func didRecordImpression() {
  nativeAd?.recordImpression()
}

func didRecordClickOnAsset(
  withName assetName: GADUnifiedNativeAssetIdentifier,
  view: UIView,
  wController: UIViewController
) {
  nativeAd?.handleClick(on: view)
}

Objective-C

- (void)didRecordImpression {
  if (self.nativeAd) {
    [self.nativeAd recordImpression];
  }
}

- (void)didRecordClickOnAssetWithName:(GADUnifiedNativeAssetIdentifier)assetName
                                 view:(UIView *)view
                       viewController:(UIViewController *)viewController {
  if (self.nativeAd) {
    [self.nativeAd handleClickOnView:view];
  }
}

เนื่องจากคลาสที่ใช้โปรโตคอล GADMediationNativeAd มีการอ้างอิงไปยังออบเจ็กต์โฆษณาเนทีฟของ SDK ตัวอย่าง จึงเรียกเมธอดที่เหมาะสมในออบเจ็กต์นั้นเพื่อรายงานการคลิกหรือการแสดงผลได้ โปรดทราบว่าเมธอด didRecordClickOnAssetWithName:view:viewController: รับพารามิเตอร์เดียว ซึ่งคือออบเจ็กต์ View ที่สอดคล้องกับชิ้นงานโฆษณาเนทีฟที่ได้รับการคลิก

ติดตามคลิกและการแสดงผลด้วย SDK ที่ใช้สื่อกลาง

SDK ที่ใช้สื่อกลางบางรายการอาจต้องการติดตามการคลิกและการแสดงผลด้วยตนเอง ในกรณีนี้ คุณควรติดตั้งใช้งานเมธอด handlesUserClicks และ handlesUserImpressions ดังที่แสดงในข้อมูลโค้ดด้านล่าง การส่งคืน YESหมายความว่าเหตุการณ์ที่กำหนดเองจะรับผิดชอบในการติดตาม เหตุการณ์เหล่านี้ และจะแจ้งให้ Google Mobile Ads SDK ทราบเมื่อเกิดเหตุการณ์เหล่านี้

เหตุการณ์ที่กำหนดเองซึ่งลบล้างการติดตามคลิกและการแสดงผลสามารถใช้ข้อความ didRenderInView: เพื่อส่งมุมมองของโฆษณาเนทีฟไปยังออบเจ็กต์โฆษณาเนทีฟของ SDK สื่อกลาง เพื่อให้ SDK สื่อกลางทำการติดตามจริงได้ SDK ตัวอย่างจากโปรเจ็กต์ตัวอย่างเหตุการณ์ที่กําหนดเอง (ซึ่งเป็นที่มาของข้อมูลโค้ดในคําแนะนํานี้) ไม่ได้ใช้วิธีนี้ แต่หากใช้ โค้ดเหตุการณ์ที่กําหนดเองจะเรียกใช้เมธอด setNativeAdView:view: ดังที่แสดงในข้อมูลโค้ดด้านล่าง

Swift

func handlesUserClicks() -> Bool {
  return true
}
func handlesUserImpressions() -> Bool {
  return true
}

func didRender(
  in view: UIView, clickableAssetViews: [GADNativeAssetIdentifier: UIView],
  nonclickableAssetViews: [GADNativeAssetIdentifier: UIView],
  viewController: UIViewController
) {
  // This method is called when the native ad view is rendered. Here you would pass the UIView
  // back to the mediated network's SDK.
  self.nativeAd?.setNativeAdView(view)
}

Objective-C

- (BOOL)handlesUserClicks {
  return YES;
}

- (BOOL)handlesUserImpressions {
  return YES;
}

- (void)didRenderInView:(UIView *)view
       clickableAssetViews:(NSDictionary<GADNativeAssetIdentifier, UIView *> *)
                               clickableAssetViews
    nonclickableAssetViews:(NSDictionary<GADNativeAssetIdentifier, UIView *> *)
                               nonclickableAssetViews
            viewController:(UIViewController *)viewController {
  // This method is called when the native ad view is rendered. Here you would
  // pass the UIView back to the mediated network's SDK. Playing video using
  // SampleNativeAd's playVideo method
  [_nativeAd setNativeAdView:view];
}

ส่งต่อเหตุการณ์สื่อกลางไปยัง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google

เมื่อเรียกใช้ GADMediationNativeLoadCompletionHandler ด้วยโฆษณาที่โหลดแล้ว อะแดปเตอร์จะใช้GADMediationNativeAdEventDelegate delegate object ที่ส่งกลับมาเพื่อส่งต่อเหตุการณ์การนำเสนอจาก SDK ของบุคคลที่สามไปยัง SDK โฆษณาในอุปกรณ์เคลื่อนที่ของ Google ได้

คุณควรส่งต่อการเรียกกลับเหล่านี้ให้มากที่สุดเท่าที่จะเป็นไปได้สำหรับเหตุการณ์ที่กำหนดเอง เพื่อให้แอปได้รับเหตุการณ์ที่เทียบเท่าจาก Google Mobile Ads SDK ตัวอย่างการใช้ฟังก์ชันเรียกกลับ

เพียงเท่านี้ก็ใช้งานเหตุการณ์ที่กำหนดเองสำหรับโฆษณาเนทีฟเสร็จเรียบร้อย ดูตัวอย่างฉบับเต็มได้ที่ GitHub