מודעות מעברונים

מודעות מעברון הן מודעות במסך מלא שמכסות את הממשק של האפליקציה עד נסגר על ידי המשתמש. הם מוצגים בדרך כלל בנקודות מעבר טבעיות את הזרימה של האפליקציה, למשל בין פעילויות או בזמן ההשהיה בין שלבים במשחק. כשאפליקציה מציגה מודעת מעברון, המשתמש יכול לבחור להקיש על המודעה ולהמשיך ליעד שלה, או לסגור אותה ולחזור לאפליקציה. מקרה לדוגמה.

במדריך הזה מוסבר איך לשלב מודעות מעברון באפליקציה ל-iOS.

דרישות מוקדמות

ביצוע בדיקות באמצעות מודעות בדיקה תמיד

כשיוצרים ובודקים אפליקציות, חשוב להשתמש במודעות בדיקה במקום במודעות בדיקה של מודעות בשידור חי. אם לא תעשו זאת, החשבון שלכם עלול להיחסם.

הדרך הקלה ביותר לטעון מודעות בדיקה היא להשתמש במזהה הייעודי של יחידת המודעות לבדיקה למודעות מעברון ב-iOS:
/21775744923/example/interstitial

הוא הוגדר במיוחד להחזרת מודעות בדיקה עבור כל בקשה, לשימוש בחינם באפליקציות שלכם תוך כדי תכנות, בדיקות וניפוי באגים. צריך רק ליצור יש להחליף אותו במזהה יחידת המודעות שלך לפני פרסום האפליקציה.

למידע נוסף על אופן הפעולה של מודעות בדיקה ב-Mobile Ads SDK, אפשר לעיין במאמר מודעות בדיקה.

הטמעה

השלבים העיקריים לשילוב מודעות מעברון הם:

  1. טוענים מודעה.
  2. הרשמה להתקשרות חזרה.
  3. מציגים את המודעה ומטפלים באירוע הפרס.

טעינת מודעה

הטעינה של מודעה מתבצעת באמצעות השיטה load(adUnitID:request) בכיתה GAMInterstitialAd.

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  private var interstitial: GAMInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    Task {
      do {
        interstitial = try await GAMInterstitialAd.load(
          withAdUnitID: "/21775744923/example/interstitial", request: GAMRequest())
      } 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) GAMInterstitialAd *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GAMRequest *request = [GAMRequest request];
  [GAMInterstitialAd loadWithAdManagerAdUnitID:@"/21775744923/example/interstitial"
      request:request
      completionHandler:^(GAMInterstitialAd *ad, NSError *error) {
    if (error) {
      NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]);
      return;
    }
    self.interstitial = ad;
  }];
}

הרשמה להתקשרות חזרה

כדי לקבל התראות על אירועי הצגה, צריך להטמיע את פרוטוקול GADFullScreenContentDelegate ולהקצות אותו למאפיין fullScreenContentDelegate של המודעה שהוחזרה. הפרוטוקול GADFullScreenContentDelegate מטפל בקריאות חוזרות (callbacks) כאשר המודעה מוצגת מוצגת בהצלחה או נכשלת, וכשהיא נסגרת. הבאים שמראה איך להטמיע את הפרוטוקול ולהקצות אותו למודעה:

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {

  private var interstitial: GAMInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    Task {
      do {
        interstitial = try await GAMInterstitialAd.load(
          withAdUnitID: "/21775744923/example/interstitial", request: GAMRequest())
        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) GAMInterstitialAd *interstitial;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  GAMRequest *request = [GAMRequest request];
  [GAMInterstitialAd loadWithAdManagerAdUnitID:@"/21775744923/example/interstitial"
      request:request
      completionHandler:^(GAMInterstitialAd *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.");
}

GAMInterstitialAd הוא אובייקט חד-פעמי. כלומר, אחרי שמודעת מעברון מוצגת, אי אפשר להציג אותה שוב. הכי טובה היא לטעון מודעת מעברון אחרת השיטה 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

ניתן להאזין לאירועים בממשק המשתמש בתצוגה כדי לקבוע מתי להציג את המודעה.

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");
}

שיטות מומלצות

כדאי לבדוק אם מודעות מעברון הן סוג המודעה המתאים לאפליקציה שלכם.
מודעות מעברון פועלות בצורה הטובה ביותר באפליקציות עם נקודות מעבר טבעיות. נקודות כאלה נוצרות בסיום משימה באפליקציה, כמו שיתוף תמונה או השלמת שלב במשחק. מכיוון שהמשתמש מצפה להפסקה בפעולה, קל להציג לו מודעת מעברון בלי להפריע לחוויית השימוש שלו. חשוב להביא בחשבון באילו נקודות בתהליך העבודה באפליקציה תוצגו מודעות מעברון ואיך סביר להניח שהמשתמשים יגיבו אליהן.
זכרו להשהות את הפעולה בזמן ההצגה של מודעת מעברון.
יש כמה סוגים שונים של מודעות מעברון: טקסט, תמונה, וידאו ועוד. חשוב לוודא שכאשר האפליקציה מציגה כמודעת מעברון, היא גם משעה את השימוש במשאבים מסוימים כדי לאפשר למודעה לנצל אותן. לדוגמה, כשמתקשרים כדי להציג מודעת מעברון, הקפידו להשהות את כל פלט האודיו שהאפליקציה שלכם יוצרת. ניתן להמשיך את השמעת הצלילים adDidDismissFullScreenContent: הגורם המטפל באירועים, שיופעל כאשר המשתמש יסיים את האינטראקציה עם המודעה. בנוסף, כדאי לעצור באופן זמני חישובים אינטנסיביים משימות שונות (כמו לולאה במשחק) בזמן שהמודעה מוצגת. הפעולה הזו תבטיח שהמשתמש לא רואה גרפיקה איטית או לא מגיבה או קטע מקוטע בסרטון.
צריך לאפשר זמן טעינה מתאים.
בדיוק כמו שחשוב לוודא שמודעות המעברון מוצגות מתאים, חשוב גם לוודא שהמשתמש לא צריך להמתין לטעינה שלהן. המערכת טוענת את המודעה מראש לפני שמתכוונים להציג אותה יכול להבטיח שבאפליקציה תהיה מודעת מעברון שנטענה במלואה, כשהיא תהיה מוכנה הגיע הזמן להציג אחת.
אסור להציף את המשתמש במודעות.
למרות שהגברת התדירות של מודעות מעברון באפליקציה שלך, זה עשוי להיראות כמו דרך נהדרת להגדיל את ההכנסות, היא גם יכולה לפגוע בחוויית המשתמש ושיעורי קליקים נמוכים יותר. ודאו שהמשתמשים לא בתדירות גבוהה במקרה שהם לא יוכלו יותר ליהנות מהשימוש באפליקציה שלך.
אין להשתמש בקריאה חוזרת להשלמת הטעינה כדי להציג את מודעת המעברון.
זה עלול לפגוע בחוויית המשתמש. במקום זאת, טוענים מראש את המודעה לפני צריכים להראות אותו. לאחר מכן צריך לבדוק את ה-method canPresentFromRootViewController:error:. ב-GAMInterstitialAd כדי לדעת אם הוא מוכן מוצגת.

דוגמאות ב-GitHub

אפשר לצפות בדוגמאות המלאות של מודעות המעברון בשפה המועדפת עליכם:

השלבים הבאים