الإعلانات البينية بمكافأة هي نوع من أشكال الإعلانات المحفَّزة التي تتيح لك تقديم مكافآت للإعلانات التي تظهر تلقائيًا أثناء عمليات الانتقال الطبيعية بين التطبيقات. على عكس الإعلانات التي تضم مكافأة، لا يُطلب من المستخدمين الموافقة على عرض الإعلانات البينية التي تضم مكافأة.
المتطلّبات الأساسية
- SDK لإعلانات Google على الأجهزة الجوّالة الإصدار 7.60.0 أو إصدار أحدث.
- أكمِل دليل البدء.
التنفيذ
في ما يلي الخطوات الأساسية لدمج الإعلانات البينية التي تضم مكافأة:
- تحميل إعلان
- [اختياري] التحقُّق من عمليات معاودة الاتصال باستخدام ميزة "التحقُّق بخطوتين"
- التسجيل لتلقّي طلبات معاودة الاتصال
- عرض الإعلان والتعامل مع حدث المكافأة
تحميل إعلان
يتم تحميل الإعلان باستخدام طريقة 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.
}];
}
الخطوات التالية
مزيد من المعلومات حول خصوصية المستخدم