الإعلان البيني بمكافأة (إصدار تجريبي)

الإعلان البيني بمكافأة هو نوع من أشكال الإعلانات المحفَّزة التي تتيح لك تقديم مكافآت مقابل الإعلانات التي تظهر تلقائيًا أثناء عمليات الانتقال الطبيعية للتطبيق. وعلى عكس الإعلانات التي تضم مكافأة، مطلوب الموافقة على عرض الإعلانات البينية التي تضم مكافأة.

المتطلبات الأساسية

  • SDK لإعلانات Google على الأجهزة الجوّالة الإصدار 7.60.0 أو إصدار أحدث.
  • أكمِل دليل البدء.

التنفيذ

في ما يلي الخطوات الأساسية لدمج الإعلانات البينية التي تضم مكافأة:

  • تحميل إعلان
  • [اختياري] التحقُّق من عمليات معاودة الاتصال باستخدام ميزة "التحقُّق بخطوتين"
  • التسجيل لتلقّي طلبات معاودة الاتصال
  • عرض الإعلان والتعامل مع حدث المكافأة

تحميل إعلان

يتم الانتهاء من تحميل إعلان باستخدام الثابت طريقة "loadWithAdUnitID:request:completionHandler:" على صف واحد (GADRewardedInterstitialAd). تتطلب طريقة التحميل رقم تعريف وحدتك الإعلانية، وهو كائن GAMRequest ومعالج عملية الإكمال والذي يتم طلبه عند نجاح تحميل الإعلان أو تعذُّر تحميله. يتوفّر عنصر GADRewardedInterstitialAd كمَعلمة في عملية الإكمال. . يوضح المثال التالي كيفية تحميل GADRewardedInterstitialAd في صف ViewController.

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
        withAdUnitID: "/21775744923/example/rewarded_interstitial", request: GAMRequest())
    } catch {
      print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
    }
  }
}

Objective-C

#import "ViewController.h"

@interface ViewController ()
@property(nonatomic, strong) GADRewardedInterstitialAd* rewardedInterstitialAd;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  [GADRewardedInterstitialAd
      loadWithAdUnitID:@"<var label='the ad unit ID'>/21775744923/example/rewarded_interstitial</var>"
                request:[GAMRequest request]
      completionHandler:^(
          GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd,
          NSError* _Nullable error) {
        if (!error) {
          self.rewardedInterstitialAd = rewardedInterstitialAd;
        }
      }
  ];
}

[اختياري] التحقق من صحة عمليات استدعاء التحقّق من جهة الخادم (SSV)

التطبيقات التي تتطلب بيانات إضافية في خادم التحقق، يجب أن تستخدم استدعاءات الخاصة بالإعلانات التي تضم مكافأة. أي قيمة سلسلة تمّ تحديدها على إعلان يضم مكافأة يتم تمرير الكائن إلى معلَمة طلب البحث custom_data لمعاودة اتصال SSV. إذا كانت الإجابة "لا" البيانات المخصّصة، ولن يتم تغيير قيمة معلَمة طلب البحث custom_data الموجودة في معاودة اتصال SSV.

يوضح نموذج الرمز البرمجي التالي كيفية ضبط البيانات المخصّصة على إعلان يضم مكافأة. عنصر الإعلان البيني قبل طلب إعلان.

Swift

do {
  rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
    withAdUnitID: "/21775744923/example/rewarded_interstitial", request: GAMRequest())
  let options = GADServerSideVerificationOptions()
  options.customRewardString = "SAMPLE_CUSTOM_DATA_STRING"
  rewardedInterstitialAd.serverSideVerificationOptions = options
} catch {
  print("Rewarded ad failed to load with error: \(error.localizedDescription)")
}

Objective-C

[GADRewardedInterstitialAd
    loadWithAdUnitID:@"/21775744923/example/rewarded_interstitial"
              request:GAMRequest
    completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) {
      if (error) {
        // Handle Error
        return;
      }
      self.rewardedInterstitialAd = ad;
      GADServerSideVerificationOptions *options =
          [[GADServerSideVerificationOptions alloc] init];
      options.customRewardString = @"SAMPLE_CUSTOM_DATA_STRING";
      ad.serverSideVerificationOptions = options;
    }];

التسجيل لتلقّي طلبات معاودة الاتصال

لتلقي إشعارات لأحداث العرض التقديمي، يجب تنفيذ بروتوكول GADFullScreenContentDelegate وتعيينه إلى السمة fullScreenContentDelegate للإعلان الذي تم عرضه. تشير رسالة الأشكال البيانية يتعامل بروتوكول GADFullScreenContentDelegate مع عمليات معاودة الاتصال عند ظهور الإعلان نجاحه أو عدم نجاحه وعندما يتم رفضه. ما يلي: طريقة تنفيذ البروتوكول وتعيينه للإعلان:

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADFullScreenContentDelegate {

  private var rewardedInterstitialAd: GADRewardedInterstitialAd?

  override func viewDidLoad() {
    super.viewDidLoad()

    do {
      rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
        withAdUnitID: "/21775744923/example/rewarded_interstitial", request: GAMRequest())
      self.rewardedInterstitialAd?.fullScreenContentDelegate = self
    } catch {
      print("Failed to load rewarded 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.")
  }
}

Objective-C

@interface ViewController () <GADFullScreenContentDelegate>
@property(nonatomic, strong) GADRewardedInterstitialAd *rewardedInterstitialAd;
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.

  [GADRewardedInterstitialAd
      loadWithAdUnitID:@"/21775744923/example/rewarded_interstitial"
                request:[GAMRequest request]
      completionHandler:^(
          GADRewardedInterstitialAd *_Nullable rewardedInterstitialAd,
          NSError *_Nullable error) {
        if (!error) {
          self.rewardedInterstitialAd = rewardedInterstitialAd;
          self.rewardedInterstitialAd.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.");
}

عرض الإعلان والتعامل مع حدث المكافأة

عند عرض إعلانك، يجب تقديم كائن GADUserDidEarnRewardHandler. لمعالجة المكافأة للمستخدم.

يقدِّم الرمز التالي أفضل طريقة لعرض إعلان بمكافأة الإعلان البيني.

Swift

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

  // The UIViewController parameter is an optional.
  rewardedInterstitialAd.present(fromRootViewController: nil) {
    let reward = rewardedInterstitialAd.adReward
    print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
    // TODO: Reward the user.
  }
}

Objective-C

- (void)show {
  // The UIViewController parameter is nullable.
  [_rewardedInterstitialAd presentFromRootViewController:nil
                                userDidEarnRewardHandler:^{

                                  GADAdReward *reward =
                                      self.rewardedInterstitialAd.adReward;
                                  // TODO: Reward the user.
                                }];
}

الخطوات التالية

مزيد من المعلومات حول خصوصية المستخدم