الإعلانات البينية التي تضم مكافأة هي نوع من أشكال الإعلانات المحفَّزة التي تتيح لك تقديم مكافآت للإعلانات التي تظهر تلقائيًا أثناء عمليات الانتقال العادية للتطبيقات. على عكس الإعلانات التي تضم مكافأة، لا يُطلب من المستخدمين الموافقة على عرض الإعلان البيني الذي يضم مكافأة.
المتطلبات الأساسية
- الإصدار 7.60.0 أو إصدار أحدث من حزمة "SDK لإعلانات Google على الأجهزة الجوّالة".
- املأ دليل البدء.
التنفيذ
في ما يلي الخطوات الأساسية لدمج الإعلانات البينية التي تضم مكافأة:
- تحميل إعلان
- [اختياري] التحقُّق من صحة عمليات معاودة الاتصال بميزة 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() GADRewardedInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/6978759866", request: GADRequest()) { ad, error in if let error = error { return print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)") } self.rewardedInterstitialAd = ad } } }
Objective-C
#import "ViewController.h" @interface ViewController () @property(nonatomic, strong) GADRewardedInterstitialAd* rewardedInterstitialAd; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [GADRewardedInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/6978759866" request:[GADRequest request] completionHandler:^( GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd, NSError* _Nullable error) { if (!error) { self.rewardedInterstitialAd = rewardedInterstitialAd; } } ]; }
[اختياري] التحقّق من صحة استدعاءات التحقّق من جهة الخادم (SSV)
إنّ التطبيقات التي تتطلب بيانات إضافية في عمليات معاودة الاتصال لإثبات الهوية من جهة الخادم يجب أن تستخدم ميزة البيانات المخصّصة للإعلانات التي تضم مكافأة. ويتم تمرير أي قيمة سلسلة يتم ضبطها في عنصر إعلان يضم مكافأة إلى معلَمة طلب البحث custom_data
الخاصة باستدعاء SSV. في حال عدم ضبط قيمة بيانات مخصّصة، لن تتوفّر قيمة مَعلمة طلب البحث custom_data
في استدعاء SSV.
يوضّح نموذج الرمز البرمجي التالي كيفية ضبط البيانات المخصّصة على عنصر إعلان بيني يضم مكافأة قبل طلب أحد الإعلانات.
Swift
GADRewardedInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/6978759866", request: request, completionHandler: { [self] ad, error in if let error != error { rewardedInterstitialAd = ad let options = GADServerSideVerificationOptions() options.customRewardString = "SAMPLE_CUSTOM_DATA_STRING" rewardedInterstitialAd.serverSideVerificationOptions = options }
Objective-C
GADRequest *request = [GADRequest request]; [GADRewardedInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/6978759866" request:request 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
class ViewController: UIViewController { private var rewardedInterstitialAd: GADRewardedInterstitialAd? override func viewDidLoad() { super.viewDidLoad() GADRewardedInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/6978759866", request: GADRequest()) { ad, error in if let error = error { return print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)") } self.rewardedInterstitialAd = ad self.rewardedInterstitialAd?.fullScreenContentDelegate = self } } } extension ViewController: GADFullScreenContentDelegate { /// 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.") } rewardedInterstitialAd.present(fromRootViewController: self) { let reward = rewardedInterstitialAd.adReward // TODO: Reward the user! } }
Objective-C
- (void)show { [_rewardedInterstitialAd presentFromRootViewController:self userDidEarnRewardHandler:^{ GADAdReward *reward = self.rewardedInterstitialAd.adReward; // TODO: Reward the user! }]; }
الخطوات التالية
مزيد من المعلومات عن خصوصية المستخدم.