מודעת מעברון מתגמלת היא סוג של פורמט מודעה מתגמלת שמופיעה באופן אוטומטי בנקודות מעבר טבעיות באפליקציה ומאפשרת לכם להציע תגמולים. בשונה ממודעות מתגמלות, המשתמשים לא צריכים להביע הסכמה לצפייה במודעת מעברון מתגמלת.
דרישות מוקדמות
- כדאי לעיין במדריך לתחילת העבודה.
הטמעה
אלה השלבים העיקריים לשילוב מודעות מעברון מתגמלות:
- טעינת מודעה
- [אופציונלי] אימות של קריאות חוזרות (callback) של SSV
- הרשמה להתקשרות חזרה
- הצגת המודעה וטיפול באירוע הפרס
טעינת מודעה
טעינת מודעה מתבצעת באמצעות השיטה load(adUnitID:request)
במחלקה GADRewardedInterstitialAd
.
Swift
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
מחליפים את adUnitID במזהה יחידת המודעות.
[אופציונלי] אימות של קריאות חוזרות (callback) של אימות בצד השרת (SSV)
באפליקציות שדורשות נתונים נוספים בקריאות חוזרות (callback) של אימות בצד השרת, צריך להשתמש בתכונת הנתונים בהתאמה אישית של מודעות מתגמלות. כל ערך מחרוזת שמוגדר באובייקט של מודעה מתגמלת מועבר לפרמטר השאילתה custom_data
של הקריאה החוזרת (callback) של אימות השרת (SSV). אם לא מוגדר ערך נתונים מותאם אישית, הערך של פרמטר השאילתה custom_data
לא יופיע בקריאה החוזרת של SSV.
דוגמת הקוד הבאה מראה איך להגדיר נתונים מותאמים אישית באובייקט של מודעה מתגמלת מסוג interstitial לפני שליחת בקשה להצגת מודעה.
Swift
Objective-C
מחליפים את SAMPLE_CUSTOM_DATA_STRING בנתונים המותאמים אישית.
הרשמה להתקשרות חזרה
כדי לקבל התראות על אירועים שקשורים להצגת מודעות, צריך להקצות את GADFullScreenContentDelegate
לנכס fullScreenContentDelegate
של המודעה שמוחזרת:
Swift
rewardedInterstitialAd?.fullScreenContentDelegate = self
SwiftUI
rewardedInterstitialAd?.fullScreenContentDelegate = self
Objective-C
self.rewardedInterstitialAd.fullScreenContentDelegate = self;
פרוטוקול GADFullScreenContentDelegate
מטפל בקריאות חוזרות (callback) כשמודעה מוצגת בהצלחה או לא, וכשהיא נסגרת. בדוגמה הבאה אפשר לראות איך מטמיעים את הפרוטוקול ומשייכים אותו למודעה:
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
אפשר לראות דוגמאות מלאות של מודעות מעברון מתגמלות בשפה המועדפת: