إعلان بيني بمكافأة

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

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

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

التنفيذ

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

  • تحميل إعلان
  • [اختياري] التحقّق من صحة استدعاءات SSV
  • التسجيل لمعاودة الاتصال
  • عرض الإعلان والتعامل مع حدث المكافأة

تحميل إعلان

يتم تحميل الإعلان باستخدام طريقة loadWithAdUnitID:request:completionHandler: الثابتة للفئة GADRewardedInterstitialAd. تتطلب طريقة التحميل رقم تعريف الوحدة الإعلانية، وعنصر GADRequest، ومعالج إكمال، ويتم استدعاؤه عند نجاح تحميل الإعلان أو تعذُّر تحميله. ويتم توفير الكائن 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: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
    } 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'>ca-app-pub-3940256099942544/6978759866</var>"
                request:[GADRequest 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: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
  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:@"ca-app-pub-3940256099942544/6978759866"
              request:GADRequest
    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: "ca-app-pub-3940256099942544/6978759866", request: GADRequest())
      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:@"ca-app-pub-3940256099942544/6978759866"
                request:[GADRequest 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.
                                }];
}

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

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