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

اختيار النظام الأساسي: Android iOS Unity Flutter Android (الإصدار القديم)

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

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

قبل المتابعة، عليك إعداد Google Mobile Ads SDK.

التنفيذ

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

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

تحميل إعلان

يتم تحميل الإعلان باستخدام load(adUnitID:request) طريقة في فئة GADRewardedInterstitialAd.

Swift

func loadRewardedInterstitialAd() async {
  do {
    rewardedInterstitialAd = try await RewardedInterstitialAd.load(
      // Replace this ad unit ID with your own ad unit ID.
      with: "adUnitID", request: Request())
    rewardedInterstitialAd?.fullScreenContentDelegate = self
  } catch {
    print("Rewarded ad failed to load with error: \(error.localizedDescription)")
  }
}

SwiftUI

import GoogleMobileAds

class RewardedInterstitialViewModel: NSObject, ObservableObject,
  FullScreenContentDelegate
{
  @Published var coins = 0
  private var rewardedInterstitialAd: RewardedInterstitialAd?

  func loadAd() async {
    do {
      rewardedInterstitialAd = try await RewardedInterstitialAd.load(
        with: "ca-app-pub-3940256099942544/6978759866", request: Request())
      rewardedInterstitialAd?.fullScreenContentDelegate = self
    } catch {
      print(
        "Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
    }
  }

Objective-C

- (void)loadRewardedInterstitialAd {
  [GADRewardedInterstitialAd loadWithAdUnitID:"adUnitID"
                                      request:[GADRequest request]
                            completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) {
                              if (error) {
                                NSLog(@"Failed to load rewarded interstitial ad with error: %@",
                                      error.localizedDescription);
                                return;
                              }
                              self.rewardedInterstitialAd = ad;
                              self.rewardedInterstitialAd.fullScreenContentDelegate = self;
                            }];
}

استبدِل adUnitID برقم تعريف وحدتك الإعلانية.

[اختياري] التحقّق من صحة معاودات الاتصال في ميزة "التحقّق من جهة الخادم"

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

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

Swift

private func validateServerSideVerification() async {
  do {
    rewardedInterstitialAd = try await RewardedInterstitialAd.load(
      // Replace this ad unit ID with your own ad unit ID.
      with: "adUnitID", request: Request())
    let options = ServerSideVerificationOptions()
    options.customRewardText = ""SAMPLE_CUSTOM_DATA_STRING""
    rewardedInterstitialAd?.serverSideVerificationOptions = options
  } catch {
    print("Rewarded ad failed to load with error: \(error.localizedDescription)")
  }
}

Objective-C

- (void)validateServerSideVerification {
  // Replace this ad unit ID with your own ad unit ID.
  [GADRewardedInterstitialAd loadWithAdUnitID:"adUnitID"
                                      request:[GADRequest request]
                            completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) {
                              if (error) {
                                NSLog(@"Rewarded interstitial ad failed to load with error: %@",
                                      error.localizedDescription);
                                return;
                              }
                              self.rewardedInterstitialAd = ad;
                              GADServerSideVerificationOptions *options =
                                  [[GADServerSideVerificationOptions alloc] init];
                              options.customRewardString = @""SAMPLE_CUSTOM_DATA_STRING"";
                              ad.serverSideVerificationOptions = options;
                            }];
}

استبدِل SAMPLE_CUSTOM_DATA_STRING ببياناتك المخصّصة.

تسجيل معاودات الاتصال

لتلقّي إشعارات بأحداث العرض، عليك تعيين GADFullScreenContentDelegate للسمة fullScreenContentDelegate للإعلان الذي تم عرضه:

Swift

rewardedInterstitialAd?.fullScreenContentDelegate = self

SwiftUI

rewardedInterstitialAd?.fullScreenContentDelegate = self

Objective-C

self.rewardedInterstitialAd.fullScreenContentDelegate = self;

تعالج بروتوكول GADFullScreenContentDelegate معاودات الاتصال عندما يتم عرض الإعلان بنجاح أو بدون نجاح، وعندما يتم إغلاقه. تعرض التعليمات البرمجية التالية كيفية تنفيذ البروتوكول وتعيينه للإعلان:

Swift

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

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

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

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

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

func ad(
  _ ad: FullScreenPresentingAd,
  didFailToPresentFullScreenContentWithError error: Error
) {
  print("\(#function) called with error: \(error.localizedDescription).")
}

SwiftUI

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

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

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

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

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

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

Objective-C

- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adWillDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
  NSLog(@"%s called", __PRETTY_FUNCTION__);
  // Clear the rewarded interstitial ad.
  self.rewardedInterstitialAd = nil;
}

- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
  NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
}

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

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

تعرض التعليمات البرمجية التالية أفضل طريقة لعرض "إعلان بيني مقابل مكافأة".

Swift

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

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

SwiftUI

يمكنك الاستماع إلى أحداث واجهة المستخدم في طريقة العرض لعرض الإعلان.

var rewardedInterstitialBody: some View {
  // ...
  }
  .onChange(
    of: showAd,
    perform: { newValue in
      if newValue {
        viewModel.showAd()
      }
    }
  )

يمكنك عرض "الإعلان البيني مقابل مكافأة" من نموذج العرض:

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

  rewardedInterstitialAd.present(from: nil) {
    let reward = rewardedInterstitialAd.adReward
    print("Reward amount: \(reward.amount)")
    self.addCoins(reward.amount.intValue)
  }
}

Objective-C

- (void)showRewardedInterstitialAd {
  [self.rewardedInterstitialAd presentFromRootViewController:self
                                    userDidEarnRewardHandler:^{
                                      GADAdReward *reward = self.rewardedInterstitialAd.adReward;

                                      NSString *rewardMessage = [NSString
                                          stringWithFormat:@"Reward received with "
                                                           @"currency %@ , amount %ld",
                                                           reward.type, [reward.amount longValue]];
                                      NSLog(@"%@", rewardMessage);
                                      // TODO: Reward the user.
                                    }];
}

أمثلة على GitHub

يمكنك الاطّلاع على أمثلة كاملة عن "الإعلانات البينية مقابل مكافأة" باللغة المفضّلة لديك:

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

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