เนทีฟขั้นสูง

การแสดงรูปแบบโฆษณาเนทีฟที่ระบบกำหนด

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

GADNativeAdView

สำหรับ GADNativeAd จะมี "การดูโฆษณา" ที่เกี่ยวข้อง ชั้นเรียน: GADNativeAdView คลาสการดูโฆษณานี้เป็นประเภท UIView ที่ผู้เผยแพร่โฆษณาควรใช้เพื่อแสดงโฆษณา ตัวอย่างเช่น GADNativeAdView เดี่ยวสามารถแสดงอินสแตนซ์เดียวของ GADNativeAd ออบเจ็กต์ UIView แต่ละรายการที่ใช้เพื่อแสดงโฆษณานั้น เนื้อหาควรเป็นมุมมองย่อยของออบเจ็กต์ GADNativeAdView ดังกล่าว

ตัวอย่างเช่น ถ้าคุณแสดงโฆษณาใน UITableView มุมมองต้นไม้ของเซลล์ใดเซลล์หนึ่งอาจมีลักษณะดังนี้

ชั้นเรียน GADNativeAdView จะมี IBOutlets ที่ใช้ในการลงทะเบียนด้วย ข้อมูลพร็อพเพอร์ตี้ที่ใช้สำหรับเนื้อหาแต่ละรายการ และวิธีการลงทะเบียน GADNativeAd ของออบเจ็กต์ดังกล่าว การบันทึกมุมมองในลักษณะนี้ทำให้ SDK สามารถ จัดการงานต่างๆ เช่น

  • กำลังบันทึกการคลิก
  • การแสดงผลการบันทึก (เมื่อพิกเซลแรกปรากฏบนหน้าจอ)
  • กำลังแสดงการวางซ้อนตัวเลือกโฆษณาอื่นๆ

การวางซ้อนตัวเลือกโฆษณาอื่นๆ

สำหรับโฆษณาเนทีฟโดยอ้อม (แสดงผ่าน Ad Manager โฆษณาทดแทน หรือผ่าน Ad Exchange หรือ AdSense) การวางซ้อน "ตัวเลือกโฆษณาอื่นๆ" จะเพิ่มเข้าไปโดย SDK โปรดเว้นที่ว่างในพื้นที่ที่คุณต้องการ มุม ของมุมมองโฆษณาเนทีฟสำหรับโลโก้ "ตัวเลือกโฆษณาอื่นๆ" ที่แทรกโดยอัตโนมัติ นอกจากนี้ให้ ตรวจสอบว่าการวางซ้อนตัวเลือกโฆษณาอื่นๆ วางบนเนื้อหาที่อนุญาตให้ไอคอน มองเห็นได้ง่าย สำหรับข้อมูลเพิ่มเติมเกี่ยวกับลักษณะและฟังก์ชันของการวางซ้อน โปรดดูที่ หลักเกณฑ์การใช้งานโฆษณาเนทีฟแบบเป็นโปรแกรม

การระบุว่าเป็นโฆษณาสำหรับโฆษณาเนทีฟแบบเป็นโปรแกรม

เมื่อแสดงโฆษณาเนทีฟแบบเป็นโปรแกรม คุณต้องแสดงการระบุว่าเป็นโฆษณาเพื่อ แสดงว่าการดูนั้นเป็นโฆษณา ดูหลักเกณฑ์ด้านนโยบายจากหน้านี้

ตัวอย่างโค้ด

มาดูวิธีแสดงโฆษณาเนทีฟโดยใช้มุมมองที่โหลดแบบไดนามิกกัน จากไฟล์ xib วิธีนี้เป็นวิธีการที่มีประโยชน์มากเมื่อใช้ GADAdLoaders มีการกำหนดค่าให้ส่งคำขอหลายรูปแบบ

จัดวาง UIViews

ขั้นตอนแรกคือการจัดเลย์เอาต์ UIViews ที่จะแสดงชิ้นงานโฆษณาเนทีฟ คุณสามารถทำสิ่งนี้ได้ในเครื่องมือสร้างอินเทอร์เฟซเช่นเดียวกับที่คุณสร้าง xib เลย์เอาต์สำหรับโฆษณาเนทีฟมีดังนี้ โฆษณาอาจมีหน้าตา:

สังเกตค่าคลาสที่กำหนดเองที่ด้านขวาบนของรูปภาพ ตั้งค่าไว้เป็น

GADNativeAdView นี่คือคลาสการดูโฆษณาที่ใช้เพื่อแสดง GADNativeAd

คุณยังจะต้องตั้งค่าชั้นเรียนที่กำหนดเองสำหรับ GADMediaView ด้วย เพื่อแสดงวิดีโอหรือรูปภาพสำหรับโฆษณา

เมื่อได้การดูแล้ว และคุณได้กำหนดคลาสการดูโฆษณาที่ถูกต้องให้กับ ให้ลิงก์ตัวเลือกชิ้นงานของมุมมองโฆษณากับ UIViews ที่คุณสร้างขึ้น ต่อไปนี้คือวิธีที่คุณสามารถลิงก์แหล่งเนื้อหาของการดูโฆษณากับ UIViews ที่สร้างขึ้น สำหรับโฆษณา

ในแผงเต้ารับ ลิงก์เต้ารับใน GADNativeAdView กับ UIViews ที่จัดวางไว้ในเครื่องมือสร้างอินเทอร์เฟซ ซึ่งช่วยให้ SDK จะรู้ว่า UIView ใดแสดงชิ้นงานใด สิ่งสำคัญอีกอย่างคือ ร้านค้าเหล่านี้แสดงถึงมุมมองที่ ที่คลิกได้ในโฆษณา

แสดงโฆษณา

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

Swift

// Mark: - GADNativeAdLoaderDelegate
func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
  print("Received native ad: \(nativeAd)")
  refreshAdButton.isEnabled = true
  // Create and place ad in view hierarchy.
  let nibView = Bundle.main.loadNibNamed("NativeAdView", owner: nil, options: nil)?.first
  guard let nativeAdView = nibView as? GADNativeAdView else {
    return
  }
  setAdView(nativeAdView)

  // Set ourselves as the native ad delegate to be notified of native ad events.
  nativeAd.delegate = self

  // Populate the native ad view with the native ad assets.
  // The headline and mediaContent are guaranteed to be present in every native ad.
  (nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline
  nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent

  // This app uses a fixed width for the GADMediaView and changes its height to match the aspect
  // ratio of the media it displays.
  if let mediaView = nativeAdView.mediaView, nativeAd.mediaContent.aspectRatio > 0 {
    let heightConstraint = NSLayoutConstraint(
      item: mediaView,
      attribute: .height,
      relatedBy: .equal,
      toItem: mediaView,
      attribute: .width,
      multiplier: CGFloat(1 / nativeAd.mediaContent.aspectRatio),
      constant: 0)
    heightConstraint.isActive = true
  }

  // These assets are not guaranteed to be present. Check that they are before
  // showing or hiding them.
  (nativeAdView.bodyView as? UILabel)?.text = nativeAd.body
  nativeAdView.bodyView?.isHidden = nativeAd.body == nil

  (nativeAdView.callToActionView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)
  nativeAdView.callToActionView?.isHidden = nativeAd.callToAction == nil

  (nativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image
  nativeAdView.iconView?.isHidden = nativeAd.icon == nil

  (nativeAdView.starRatingView as? UIImageView)?.image = imageOfStars(
    fromStarRating: nativeAd.starRating)
  nativeAdView.starRatingView?.isHidden = nativeAd.starRating == nil

  (nativeAdView.storeView as? UILabel)?.text = nativeAd.store
  nativeAdView.storeView?.isHidden = nativeAd.store == nil

  (nativeAdView.priceView as? UILabel)?.text = nativeAd.price
  nativeAdView.priceView?.isHidden = nativeAd.price == nil

  (nativeAdView.advertiserView as? UILabel)?.text = nativeAd.advertiser
  nativeAdView.advertiserView?.isHidden = nativeAd.advertiser == nil

  // For the SDK to process touch events properly, user interaction should be disabled.
  nativeAdView.callToActionView?.isUserInteractionEnabled = false

  // Associate the native ad view with the native ad object. This is
  // required to make the ad clickable.
  // Note: this should always be done after populating the ad views.
  nativeAdView.nativeAd = nativeAd
}

SwiftUI

สร้างโมเดลการแสดงผล

สร้างโมเดลข้อมูลพร็อพเพอร์ตี้ที่โหลดโฆษณาเนทีฟ และเผยแพร่ข้อมูลโฆษณาเนทีฟ การเปลี่ยนแปลง:

import GoogleMobileAds

class NativeAdViewModel: NSObject, ObservableObject, GADNativeAdLoaderDelegate {
  @Published var nativeAd: GADNativeAd?
  private var adLoader: GADAdLoader!

  func refreshAd() {
    adLoader = GADAdLoader(
      adUnitID: "ca-app-pub-3940256099942544/3986624511",
      // The UIViewController parameter is optional.
      rootViewController: nil,
      adTypes: [.native], options: nil)
    adLoader.delegate = self
    adLoader.load(GADRequest())
  }

  func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
    // Native ad data changes are published to its subscribers.
    self.nativeAd = nativeAd
    nativeAd.delegate = self
  }

  func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: Error) {
    print("\(adLoader) failed with error: \(error.localizedDescription)")
  }
}

สร้าง UIViewAssignable

สร้าง UIViewRepresentable สำหรับ GADNativeView และสมัครรับข้อมูลการเปลี่ยนแปลงข้อมูลใน ViewModel ชั้นเรียน:

private struct NativeAdView: UIViewRepresentable {
  typealias UIViewType = GADNativeAdView

  // Observer to update the UIView when the native ad value changes.
  @ObservedObject var nativeViewModel: NativeAdViewModel

  func makeUIView(context: Context) -> GADNativeAdView {
    return
      Bundle.main.loadNibNamed(
        "NativeAdView",
        owner: nil,
        options: nil)?.first as! GADNativeAdView
  }

  func updateUIView(_ nativeAdView: GADNativeAdView, context: Context) {
    guard let nativeAd = nativeViewModel.nativeAd else { return }

    // Each UI property is configurable using your native ad.
    (nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline

    nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent

    (nativeAdView.bodyView as? UILabel)?.text = nativeAd.body

    (nativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image

    (nativeAdView.starRatingView as? UIImageView)?.image = imageOfStars(from: nativeAd.starRating)

    (nativeAdView.storeView as? UILabel)?.text = nativeAd.store

    (nativeAdView.priceView as? UILabel)?.text = nativeAd.price

    (nativeAdView.advertiserView as? UILabel)?.text = nativeAd.advertiser

    (nativeAdView.callToActionView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)

    // For the SDK to process touch events properly, user interaction should be disabled.
    nativeAdView.callToActionView?.isUserInteractionEnabled = false

    // Associate the native ad view with the native ad object. This is required to make the ad
    // clickable.
    // Note: this should always be done after populating the ad views.
    nativeAdView.nativeAd = nativeAd
  }

เพิ่มมุมมองไปยังลำดับชั้นการแสดงผล

โค้ดต่อไปนี้แสดงการเพิ่ม UIViewRepresentable ลงในข้อมูลพร็อพเพอร์ตี้ ลำดับชั้น:

struct NativeContentView: View {
  // Single source of truth for the native ad data.
  @StateObject private var nativeViewModel = NativeAdViewModel()

  var body: some View {
    ScrollView {
      VStack(spacing: 20) {
        NativeAdView(nativeViewModel: nativeViewModel)  // Updates when the native ad data changes.
          .frame(minHeight: 300)  // minHeight determined from xib.

Objective-C

#pragma mark GADNativeAdLoaderDelegate implementation

- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativeAd {
  NSLog(@"Received native ad: %@", nativeAd);
  self.refreshButton.enabled = YES;

  // Create and place ad in view hierarchy.
  GADNativeAdView *nativeAdView =
      [[NSBundle mainBundle] loadNibNamed:@"NativeAdView" owner:nil options:nil].firstObject;
  [self setAdView:nativeAdView];

  // Set the mediaContent on the GADMediaView to populate it with available
  // video/image asset.
  nativeAdView.mediaView.mediaContent = nativeAd.mediaContent;

  // Populate the native ad view with the native ad assets.
  // The headline is guaranteed to be present in every native ad.
  ((UILabel *)nativeAdView.headlineView).text = nativeAd.headline;

  // These assets are not guaranteed to be present. Check that they are before
  // showing or hiding them.
  ((UILabel *)nativeAdView.bodyView).text = nativeAd.body;
  nativeAdView.bodyView.hidden = nativeAd.body ? NO : YES;

  [((UIButton *)nativeAdView.callToActionView)setTitle:nativeAd.callToAction
                                                forState:UIControlStateNormal];
  nativeAdView.callToActionView.hidden = nativeAd.callToAction ? NO : YES;

    ((UIImageView *)nativeAdView.iconView).image = nativeAd.icon.image;
  nativeAdView.iconView.hidden = nativeAd.icon ? NO : YES;

  ((UIImageView *)nativeAdView.starRatingView).image = [self imageForStars:nativeAd.starRating];
  nativeAdView.starRatingView.hidden = nativeAd.starRating ? NO : YES;

  ((UILabel *)nativeAdView.storeView).text = nativeAd.store;
  nativeAdView.storeView.hidden = nativeAd.store ? NO : YES;

  ((UILabel *)nativeAdView.priceView).text = nativeAd.price;
  nativeAdView.priceView.hidden = nativeAd.price ? NO : YES;

  ((UILabel *)nativeAdView.advertiserView).text = nativeAd.advertiser;
  nativeAdView.advertiserView.hidden = nativeAd.advertiser ? NO : YES;

  // In order for the SDK to process touch events properly, user interaction
  // should be disabled.
  nativeAdView.callToActionView.userInteractionEnabled = NO;

  // Associate the native ad view with the native ad object. This is
  // required to make the ad clickable.
  nativeAdView.nativeAd = nativeAd;
}

ตัวอย่างที่สมบูรณ์ใน GitHub

ดูตัวอย่างทั้งหมดของการผสานรวมโฆษณาเนทีฟใน Swift, SwiftUI และ Objective-C โดยไปที่ลิงก์ GitHub ที่เกี่ยวข้อง

ตัวอย่างการแสดงผลที่กำหนดเองของ Swift ตัวอย่างโฆษณาเนทีฟของ SwiftUI ตัวอย่างการแสดงผลที่กำหนดเองสำหรับ Objective-C

GADMediaView

เนื้อหารูปภาพและวิดีโอแสดงต่อผู้ใช้ผ่านทาง GADMediaView นี่คือ UIView ที่กำหนดในไฟล์ xib หรือสร้างขึ้นแบบไดนามิกได้ ควรวางอยู่ในลำดับชั้นการแสดงผลของ GADNativeAdView เช่นเดียวกับ มุมมองเนื้อหาอื่นๆ

เช่นเดียวกับการดูเนื้อหาทั้งหมด มุมมองสื่อจำเป็นต้องมีเนื้อหา ป้อนข้อมูลแล้ว ซึ่งตั้งค่าโดยใช้ mediaContent ใน GADMediaView พร็อพเพอร์ตี้ mediaContent ของ GADNativeAd มีเนื้อหาสื่อที่สามารถส่งผ่านไปยัง GADMediaView

นี่คือตัวอย่างข้อมูลจาก ตัวอย่างการแสดงผลที่กำหนดเอง (Swift | Objective-C) ที่แสดงวิธีป้อนข้อมูลใน GADMediaView ด้วยเนื้อหาโฆษณาเนทีฟโดยใช้ GADMediaContent จาก GADNativeAd:

Swift

nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent

Objective-C

nativeAdView.mediaView.mediaContent = nativeAd.mediaContent;

ตรวจสอบว่าในไฟล์เครื่องมือสร้างอินเทอร์เฟซสำหรับมุมมองโฆษณาเนทีฟ ตั้งค่า ดูคลาสที่กำหนดเองเป็น GADMediaView และคุณได้เชื่อมต่อกับ เต้ารับ mediaView

การเปลี่ยนโหมดเนื้อหารูปภาพ

คลาส GADMediaView ให้ความสําคัญกับ UIView contentMode เมื่อแสดงรูปภาพ ถ้าต้องการเปลี่ยนวิธีปรับสัดส่วนรูปภาพ GADMediaView ให้กำหนดค่า UIViewContentMode ในพร็อพเพอร์ตี้ contentMode ของ GADMediaView เพื่อบรรลุเป้าหมายนี้

ตัวอย่างเช่น เพื่อเติม GADMediaView เมื่อรูปภาพปรากฏขึ้น (โฆษณาไม่มี วิดีโอ)

Swift

nativeAdView.mediaView?.contentMode = .aspectFill

Objective-C

nativeAdView.mediaView.contentMode = UIViewContentModeAspectFill;

GADMediaContent

GADMediaContent จะเก็บข้อมูลที่เกี่ยวข้องกับเนื้อหาสื่อของโฆษณาเนทีฟ ซึ่งก็คือ แสดงโดยใช้คลาส GADMediaView เมื่อตั้งค่าใน GADMediaView พร็อพเพอร์ตี้ mediaContent:

  • หากมีเนื้อหาวิดีโอพร้อมใช้งาน ระบบจะบัฟเฟอร์และเริ่มเล่นภายใน GADMediaView คุณจะทราบได้ว่าเนื้อหาวิดีโอพร้อมใช้งานหรือไม่โดยตรวจสอบ hasVideoContent

  • หากโฆษณาไม่มีชิ้นงานวิดีโอ ระบบจะดาวน์โหลดชิ้นงาน mainImage และวางอยู่ใน GADMediaView แทน

ขั้นตอนถัดไป

ดูข้อมูลเพิ่มเติมเกี่ยวกับความเป็นส่วนตัวของผู้ใช้