الإعلانات البينية بمكافأة هي نوع من أشكال الإعلانات المحفَّزة التي تتيح لك تقديم مكافآت للإعلانات التي تظهر تلقائيًا أثناء عمليات نقل التطبيقات العادية. على عكس الإعلانات بمكافأة، لا يُطلب من المستخدمين الموافقة على مشاهدة الإعلان البيني.
المتطلبات الأساسية
- 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() 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! }]; }