โฆษณาคั่นระหว่างหน้า

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

คู่มือนี้จะแสดงวิธีผสานรวมโฆษณาคั่นระหว่างหน้าเข้ากับแอป iOS

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

ทดสอบด้วยโฆษณาทดสอบเสมอ

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

วิธีที่ง่ายที่สุดในการโหลดโฆษณาทดสอบคือการใช้รหัสหน่วยโฆษณาทดสอบโดยเฉพาะของเรา สำหรับโฆษณาคั่นระหว่างหน้าบน iOS:
วันที่ ca-app-pub-3940256099942544/4411468910

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

สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีการทำงานของโฆษณาทดสอบของ SDK โฆษณาบนอุปกรณ์เคลื่อนที่ โปรดดู Test Ads

การใช้งาน

ขั้นตอนหลักในการผสานรวมโฆษณาคั่นระหว่างหน้ามีดังนี้

  1. โหลดโฆษณา
  2. ลงทะเบียนสำหรับการติดต่อกลับ
  3. แสดงโฆษณาและจัดการกิจกรรมมอบรางวัล

โหลดโฆษณา

การโหลดโฆษณาดำเนินการได้โดยใช้ load(adUnitID:request) ใน GADInterstitialAd ชั้นเรียน

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  private var interstitial: GADInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      interstitial = try await GADInterstitialAd.load(
        withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: GADRequest())
    } catch {
      print("Failed to load interstitial ad with error: \(error.localizedDescription)")
    }
  }
}

SwiftUI

import GoogleMobileAds

class InterstitialViewModel: NSObject, GADFullScreenContentDelegate {
  private var interstitialAd: GADInterstitialAd?

  func loadAd() async {
    do {
      interstitialAd = try await GADInterstitialAd.load(
        withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: GADRequest())
      interstitialAd?.fullScreenContentDelegate = self
    } catch {
      print("Failed to load interstitial ad with error: \(error.localizedDescription)")
    }
  }

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController ()

@property(nonatomic, strong) GADInterstitialAd *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GADRequest *request = [GADRequest request];
  [GADInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"
      request:request
      completionHandler:^(GADInterstitialAd *ad, NSError *error) {
    if (error) {
      NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]);
      return;
    }
    self.interstitial = ad;
  }];
}

ลงทะเบียนสำหรับการติดต่อกลับ

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

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {

  private var interstitial: GADInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      interstitial = try await GADInterstitialAd.load(
        withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: GADRequest())
      interstitial?.fullScreenContentDelegate = self
    } catch {
      print("Failed to load interstitial ad with error: \(error.localizedDescription)")
    }
  }

  /// Tells the delegate that the ad failed to present full screen content.
  func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
    print("Ad did fail to present full screen content.")
  }

  /// Tells the delegate that the ad will present full screen content.
  func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Ad will present full screen content.")
  }

  /// Tells the delegate that the ad dismissed full screen content.
  func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
    print("Ad did dismiss full screen content.")
  }
}

SwiftUI

กำหนดพร็อพเพอร์ตี้ fullScreenContentDelegate ให้กับโฆษณาที่ส่งคืน

interstitialAd?.fullScreenContentDelegate = self

ใช้โปรโตคอลดังนี้

func adDidRecordImpression(_ ad: GADFullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidRecordClick(_ ad: GADFullScreenPresentingAd) {
  print("\(#function) called")
}

func ad(
  _ ad: GADFullScreenPresentingAd,
  didFailToPresentFullScreenContentWithError error: Error
) {
  print("\(#function) called")
}

func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("\(#function) called")
}

func adWillDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("\(#function) called")
}

func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
  print("\(#function) called")
  // Clear the interstitial ad.
  interstitialAd = nil
}

Objective-C

@import GoogleMobileAds;
@import UIKit;

@interface ViewController () <GADFullScreenContentDelegate>

@property(nonatomic, strong) GADInterstitialAd *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GADRequest *request = [GADRequest request];
  [GADInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"
      request:request
      completionHandler:^(GADInterstitialAd *ad, NSError *error) {
    if (error) {
      NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]);
      return;
    }
    self.interstitial = ad;
    self.interstitial.fullScreenContentDelegate = self;
  }];
}

/// Tells the delegate that the ad failed to present full screen content.
- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
    NSLog(@"Ad did fail to present full screen content.");
}

/// Tells the delegate that the ad will present full screen content.
- (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
    NSLog(@"Ad will present full screen content.");
}

/// Tells the delegate that the ad dismissed full screen content.
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
  NSLog(@"Ad did dismiss full screen content.");
}

GADInterstitialAd เป็นออบเจ็กต์แบบใช้ครั้งเดียว ช่วงเวลานี้ หมายความว่าเมื่อมีการแสดงโฆษณาคั่นระหว่างหน้าแล้ว จะไม่สามารถแสดงได้อีก ที่ดีที่สุด คือให้โหลดโฆษณาคั่นระหว่างหน้าอีกรายการใน adDidDismissFullScreenContent:เมธอดใน GADFullScreenContentDelegate ดังนั้น ที่โฆษณาคั่นระหว่างหน้าตัวถัดไปจะเริ่มโหลดทันทีเมื่อ ปิดแล้ว

แสดงโฆษณา

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

Swift

guard let interstitial = interstitial else {
  return print("Ad wasn't ready.")
}

// The UIViewController parameter is an optional.
interstitial.present(fromRootViewController: nil)

SwiftUI

ฟังเหตุการณ์ UI ในมุมมองเพื่อกำหนดว่าควรแสดงโฆษณาเมื่อใด

var body: some View {
  // ...
  }
  .onChange(of: countdownTimer.isComplete) { newValue in
    showGameOverAlert = newValue
  }
  .alert(isPresented: $showGameOverAlert) {
    Alert(
      title: Text("Game Over"),
      message: Text("You lasted \(countdownTimer.countdownTime) seconds"),
      dismissButton: .cancel(
        Text("OK"),
        action: {
          viewModel.showAd()
        }))

แสดงโฆษณาคั่นระหว่างหน้าจากรูปแบบการดู ดังนี้

func showAd() {
  guard let interstitialAd = interstitialAd else {
    return print("Ad wasn't ready.")
  }

  interstitialAd.present(fromRootViewController: nil)
}

Objective-C

if (self.interstitial) {
  // The UIViewController parameter is nullable.
  [self.interstitial presentFromRootViewController:nil];
} else {
  NSLog(@"Ad wasn't ready");
}

แนวทางปฏิบัติแนะนำ

พิจารณาว่าโฆษณาคั่นระหว่างหน้าเป็นประเภทโฆษณาที่เหมาะสมสำหรับแอปของคุณหรือไม่
โฆษณาคั่นระหว่างหน้าทำงานได้ดีที่สุดในแอปที่มีจุดเปลี่ยนที่เป็นธรรมชาติ บทสรุปของงานภายในแอป เช่น การแชร์รูปภาพหรือการเสร็จสิ้น ในระดับเกม จะทำให้เกิดคะแนนดังกล่าว เนื่องจากผู้ใช้คาดว่าจะได้พักใน ก็สามารถนำเสนอโฆษณาคั่นระหว่างหน้าได้ง่ายๆ โดยไม่ขัดจังหวะ ประสบการณ์การใช้งาน ตรวจสอบให้แน่ใจว่าคุณพิจารณาว่าคุณต้องการไปที่จุดใดในเวิร์กโฟลว์ของแอป โฆษณาคั่นระหว่างหน้า Display และวิธีการตอบสนองของผู้ใช้
อย่าลืมหยุดการดำเนินการไว้ชั่วคราวเมื่อแสดงโฆษณาคั่นระหว่างหน้า
โฆษณาคั่นระหว่างหน้ามีหลายประเภท ได้แก่ แบบข้อความ รูปภาพ วิดีโอ และอื่นๆ คุณต้องตรวจสอบว่าเมื่อแอปแสดง โฆษณาคั่นระหว่างหน้าได้ ก็ยังระงับการใช้ทรัพยากรบางอย่างเพื่ออนุญาตให้โฆษณา ใช้ประโยชน์จากส่วนนี้ ตัวอย่างเช่น เมื่อคุณโทรออกเพื่อแสดง โฆษณาคั่นระหว่างหน้า อย่าลืมหยุดเอาต์พุตเสียงที่แอปของคุณสร้างไว้ชั่วคราว คุณกลับมาเล่นเสียงได้อีกครั้งใน adDidDismissFullScreenContent: เครื่องจัดการกิจกรรม ซึ่งจะถูกเรียกเมื่อผู้ใช้โต้ตอบเสร็จแล้ว กับโฆษณา นอกจากนี้ โปรดพิจารณาหยุดการคำนวณที่ซับซ้อนอย่างหนักไว้ชั่วคราว งานต่างๆ (เช่น Game Loop) ขณะที่โฆษณาแสดงอยู่ วิธีนี้จะช่วย เพื่อที่ผู้ใช้จะไม่พบภาพกราฟิกช้าหรือไม่ตอบสนอง หรือติดขัด
เผื่อเวลาโหลดให้เพียงพอ
คุณควรตรวจสอบว่าได้แสดงโฆษณาคั่นระหว่างหน้าที่ ในเวลาที่เหมาะสม คุณยังควรตรวจสอบว่าผู้ใช้ไม่ต้อง เพื่อรอให้ส่วนขยายโหลดขึ้นมา โหลดโฆษณาล่วงหน้าก่อนที่คุณจะตั้งใจจะแสดง จะมั่นใจได้ว่าแอปแสดงโฆษณาคั่นระหว่างหน้าที่โหลดอย่างสมบูรณ์ได้ทันท่วงที เวลาที่ต้องแสดง
อย่าแสดงโฆษณาต่อผู้ใช้จำนวนมาก
แม้ว่าการเพิ่มความถี่ของโฆษณาคั่นระหว่างหน้าในแอปอาจดูเหมือน เป็นวิธีที่ยอดเยี่ยมในการเพิ่มรายได้ ยังทำให้ประสบการณ์ของผู้ใช้แย่ลง และอัตราการคลิกผ่านลดลง ตรวจสอบว่าผู้ใช้ไม่ได้เข้าร่วมบ่อย ว่าจะไม่สามารถใช้ประโยชน์จากแอปของคุณได้อีก
อย่าใช้ Callback ที่โหลดเสร็จสมบูรณ์เพื่อแสดงโฆษณาคั่นระหว่างหน้า
การดำเนินการนี้อาจทำให้ผู้ใช้ได้รับประสบการณ์การใช้งานที่ไม่ดี แต่ให้โหลดโฆษณาไว้ล่วงหน้าก่อน ต้องแสดงให้ดู จากนั้นตรวจสอบเมธอด canPresentFromRootViewController:error: ใน GADInterstitialAd เพื่อดูว่าพร้อมเปิดตัวไหม แสดงอยู่

แหล่งข้อมูลเพิ่มเติม

ตัวอย่างใน GitHub

ดูตัวอย่างโฆษณาคั่นระหว่างหน้าแบบเต็มในภาษาที่คุณต้องการ

วิดีโอแนะนำ Mobile Ads Garage

เรื่องราวความสำเร็จ

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