بینابینی پاداش

بینابینی پاداش‌دار نوعی قالب تبلیغاتی است که به شما امکان می‌دهد برای تبلیغاتی که به‌طور خودکار در طول انتقال طبیعی برنامه ظاهر می‌شوند، پاداش ارائه دهید. برخلاف آگهی‌های دارای پاداش، کاربران مجبور نیستند برای مشاهده یک بینابینی دارای پاداش، شرکت کنند.

پیش نیازها

پیاده سازی

مراحل اولیه برای ادغام تبلیغات بینابینی دارای پاداش به شرح زیر است:

  • یک تبلیغ را بارگیری کنید
  • [اختیاری] اعتبار سنجی تماس های SSV
  • برای پاسخ به تماس ها ثبت نام کنید
  • تبلیغ را نمایش دهید و رویداد پاداش را مدیریت کنید

یک تبلیغ را بارگیری کنید

بارگیری یک تبلیغ با استفاده از روش استاتیک loadWithAdUnitID:request:completionHandler: در کلاس GADRewardedInterstitialAd انجام می شود. روش بارگیری به شناسه واحد تبلیغات شما، یک شی GADRequest و یک کنترل کننده تکمیل نیاز دارد که در صورت موفقیت یا عدم موفقیت بارگیری آگهی فراخوانی می شود. شیء بارگیری شده GADRewardedInterstitialAd به عنوان یک پارامتر در کنترل کننده تکمیل ارائه می شود. مثال زیر نحوه بارگیری GADRewardedInterstitialAd را در کلاس ViewController نشان می دهد.

سریع

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

هدف-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) تماس های برگشتی

برنامه‌هایی که به داده‌های اضافی در تماس‌های تأیید سمت سرور نیاز دارند، باید از ویژگی داده سفارشی تبلیغات پاداش استفاده کنند. هر مقدار رشته تنظیم شده روی یک شیء تبلیغاتی پاداش داده شده به پارامتر query custom_data در SSV ارسال می شود. اگر مقدار داده سفارشی تنظیم نشده باشد، مقدار پارامتر query custom_data در پاسخ تماس SSV وجود نخواهد داشت.

نمونه کد زیر نشان می‌دهد که چگونه داده‌های سفارشی را بر روی یک شیء تبلیغاتی با پاداش قبل از درخواست آگهی تنظیم کنید.

سریع

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

هدف-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 برای زمانی که تبلیغ با موفقیت یا ناموفق نمایش داده می شود و زمانی که رد می شود، پاسخ به تماس ها را کنترل می کند. کد زیر نحوه پیاده سازی پروتکل و اختصاص آن به تبلیغ را نشان می دهد:

سریع

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

هدف-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 برای مدیریت پاداش برای کاربر ارائه دهید.

کد زیر بهترین روش را برای نمایش یک تبلیغ بینابینی با پاداش ارائه می دهد.

سریع

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.
  }
}

هدف-C

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

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

مراحل بعدی

درباره حریم خصوصی کاربر بیشتر بیاموزید.