獎勵廣告是使用者可選擇與換取應用程式內獎勵互動的廣告。本指南將說明如何整合獎勵廣告 AdMob 。 閱讀客戶成功案例: 個案研究 1 、 個案研究 2 。
事前準備
- Google Mobile Ads SDK 8.0.0 以上版本
- 按照入門指南匯入 Google Mobile Ads SDK。
一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非實際上線廣告。否則我們可能會將您的帳戶停權。
如要載入測試廣告,最簡單的方法就是使用 iOS 獎勵廣告專用的專屬測試廣告單元 ID:
ca-app-pub-3940256099942544/1712485313
這已經特別針對個別請求傳回測試廣告,您可以在編寫、測試及偵錯時,在自己的應用程式中使用這個廣告格式。發布應用程式前,請務必用您自己的廣告單元 ID 取代它。
如要進一步瞭解 Mobile Ads SDK 的測試廣告運作方式,請參閱測試廣告。
導入作業
整合獎勵廣告的主要步驟如下:
- 載入廣告。
- 註冊回呼。
- 顯示廣告並處理獎勵事件。
載入廣告
在 GADRewardedAd
類別中,使用靜態 loadWithAdUnitID:request:completionHandler:
方法載入廣告即可完成。載入方法需要您的廣告單元 ID、GADRequest
物件,以及廣告載入成功或失敗時呼叫的完成處理常式。載入的 GADRewardedAd
物件會以完成參數的形式當做參數提供。以下範例顯示如何在 ViewController
類別中載入 GADRewardedAd
。
Swift
import GoogleMobileAds import UIKit class ViewController: UIViewController { private var rewardedAd: GADRewardedAd? func loadRewardedAd() { let request = GADRequest() GADRewardedAd.load(withAdUnitID:"ca-app-pub-3940256099942544/1712485313", request: request, completionHandler: { [self] ad, error in if let error = error { print("Failed to load rewarded ad with error: \(error.localizedDescription)") return } rewardedAd = ad print("Rewarded ad loaded.") } ) } }
Objective-C
@import GoogleMobileAds; @import UIKit; @interface ViewController () @property(nonatomic, strong) GADRewardedAd *rewardedAd; @end @implementation ViewController - (void)loadRewardedAd { GADRequest *request = [GADRequest request]; [GADRewardedAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/1712485313" request:request completionHandler:^(GADRewardedAd *ad, NSError *error) { if (error) { NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]); return; } self.rewardedAd = ad; NSLog(@"Rewarded ad loaded."); }]; }
註冊回呼
如要接收簡報事件的通知,您必須導入 GADFullScreenContentDelegate
通訊協定並將其指派給傳回的廣告的 fullScreenContentDelegate
屬性。GADFullScreenContentDelegate
通訊協定會處理廣告成功或失敗的時間,並會在關閉時處理回呼。以下程式碼示範如何導入通訊協定並指派給廣告:
Swift
class ViewController: UIViewController, GADFullScreenContentDelegate { private var rewardedAd: GADRewardedAd? func loadRewardedAd() { let request = GADRequest() GADRewarded.load(withAdUnitID:"ca-app-pub-3940256099942544/1712485313", request: request, completionHandler: { [self] ad, error in if let error = error { print("Failed to load rewarded ad with error: \(error.localizedDescription)") return } rewardedAd = ad print("Rewarded ad loaded.") rewardedAd?.fullScreenContentDelegate = self } ) } /// 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) GADRewardedAd *rewardedAd; @end @implementation ViewController - (void)loadRewardedAd { GADRequest *request = [GADRequest request]; [GADRewardedAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/4806952744" request:request completionHandler:^(GADRewardedAd *ad, NSError *error) { if (error) { NSLog(@"Rewarded ad failed to load with error: %@", [error localizedDescription]); return; } self.rewardedAd = ad; NSLog(@"Rewarded ad loaded."); self.rewardedAd.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."); }
GADRewardedAd
是一次性物件。這表示一旦顯示獎勵廣告,就無法再次顯示。最佳做法是在 GADFullScreenContentDelegate
的 adDidDismissFullScreenContent:
方法中載入另一則獎勵廣告,以便在下一則廣告關閉時,盡快開始載入下一則獎勵廣告。
顯示廣告並處理獎勵事件
向使用者顯示獎勵廣告前,您必須向使用者顯示明確的選擇,以換取獎勵以換取獎勵。獎勵廣告必須永遠選擇加入
顯示廣告時,您必須提供 GADUserDidEarnRewardHandler
物件,以便處理使用者的獎勵。
以下程式碼是顯示獎勵廣告的最佳方法。
Swift
func show() { if let ad = rewardedAd { ad.present(fromRootViewController: self) { let reward = ad.adReward print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)") // TODO: Reward the user. } } else { print("Ad wasn't ready") } }
Objective-C
- (void)show { ... if (self.rewardedAd) { [self.rewardedAd presentFromRootViewController:self userDidEarnRewardHandler:^{ GADAdReward *reward = self.rewardedAd.adReward; // TODO: Reward the user! }]; } else { NSLog(@"Ad wasn't ready"); } }
常見問題
- 我可以取得
GADRewardedAd
的獎勵詳情嗎? - 是,如果您要在
userDidEarnReward
回呼之前觸發獎勵金額,GADRewardedAd
有一個adReward
屬性,您可以在廣告載入後確認獎勵金額。 - 初始化呼叫是否逾時?
- 10 秒後,Google Mobile Ads SDK 會叫用
GADInitializationCompletionHandler
已提供給startWithCompletionHandler:
方法,即使中介服務聯播網尚未完成初始化。 - 收到初始化回呼時,如果某些中介服務聯播網尚未就緒,該怎麼辦?
最佳做法是在
GADInitializationCompletionHandler
中載入廣告。即使中介服務聯播網尚未就緒,Google Mobile Ads SDK 仍會請求該聯播網請求廣告。因此,如果中介服務聯播網在逾時時間結束後完成初始化,該工作階段仍可繼續處理該工作階段中的後續廣告請求。您可以呼叫
GADMobileAds.initializationStatus
,繼續在整個應用程式工作階段中輪詢所有轉接程式的初始化狀態。- 如何找出特定中介服務聯播網尚未準備就緒的原因?
GADAdapterStatus
物件的description
屬性說明瞭轉接器尚未準備好處理廣告請求的原因。
GitHub 範例
- 獎勵廣告範例: Swift | Objective-C