插頁式獎勵廣告是一種獎勵廣告格式,會在應用程式自然切換畫面時自動出現,並向使用者提供獎勵。這類廣告與一般獎勵廣告不同,使用者無需主動選擇即可觀看。
必備條件
- 完整閱讀入門指南。
導入方式
整合插頁式獎勵廣告的主要步驟如下:
- 載入廣告
- [選用] 驗證 SSV 回呼
- 註冊回呼
- 顯示廣告並處理獎勵事件
載入廣告
載入廣告時,請使用 GADRewardedInterstitialAd
類別的 load(adUnitID:request)
方法。
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 替換為廣告單元 ID,
[選用] 驗證伺服器端驗證 (SSV) 回呼
在伺服器端驗證回呼中,如果應用程式需要加入額外資料,請使用獎勵廣告的自訂資料功能。在獎勵廣告物件上設定的任何字串值,都會傳遞至 SSV 回呼的 custom_data
查詢參數。如未設定任何自訂資料值,SSV 回呼就不會包含 custom_data
查詢參數值。
請參考下列程式碼範例,瞭解如何在請求廣告前,於插頁式獎勵廣告物件上設定自訂資料。
Swift
Objective-C
將 SAMPLE_CUSTOM_DATA_STRING 替換成自訂資料。
註冊回呼
如要接收廣告顯示事件的通知,請務必將 GADFullScreenContentDelegate
指派給傳回廣告的 fullScreenContentDelegate
屬性:
Swift
rewardedInterstitialAd?.fullScreenContentDelegate = self
SwiftUI
rewardedInterstitialAd?.fullScreenContentDelegate = self
Objective-C
self.rewardedInterstitialAd.fullScreenContentDelegate = self;
GADFullScreenContentDelegate
通訊協定會處理廣告顯示成功、失敗及關閉時的回呼。以下程式碼示範如何導入協定並指派給廣告:
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
監聽檢視區塊中的 UI 事件,判斷何時顯示廣告。
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 範例程式碼
您可以依照偏好的程式語言,查看完整的插頁式獎勵廣告範例:
後續步驟
進一步瞭解使用者隱私。