Skip to content

Commit 1829ee4

Browse files
2semclaude
andauthored
feat: improve reward ad support with earn-reward tracking (#15)
- Add `didEarnRewardForUnit` delegate callback (with default no-op) - Track per-unit reward earned state via `rewardedUnits` dictionary - Reset reward state on present; set true in `userDidEarnRewardHandler` - Completion Bool now reflects actual reward earned for reward units (non-reward units continue to pass true on dismiss as before) Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 16ec223 commit 1829ee4

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

GADManager/GADManager.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public protocol GADManagerDelegate : NSObjectProtocol{
1919
func GAD<E>(manager: GADManager<E>, updatShownTimeForUnit unit: E, showTime time: Date);
2020
func GAD<E>(manager: GADManager<E>, willPresentADForUnit unit: E);
2121
func GAD<E>(manager: GADManager<E>, didDismissADForUnit unit: E);
22+
func GAD<E>(manager: GADManager<E>, didEarnRewardForUnit unit: E, reward: GoogleMobileAds.AdReward);
2223
}
2324

2425
public extension GADManagerDelegate{
@@ -28,6 +29,7 @@ public extension GADManagerDelegate{
2829
func GAD<E>(manager: GADManager<E>, updateLastPreparedTimeForUnit unit: E, preparedTime time: Date){}
2930
func GAD<E>(manager: GADManager<E>, willPresentADForUnit unit: E){}
3031
func GAD<E>(manager: GADManager<E>, didDismissADForUnit unit: E){}
32+
func GAD<E>(manager: GADManager<E>, didEarnRewardForUnit unit: E, reward: GoogleMobileAds.AdReward){}
3133
}
3234

3335
public class GADManager<E : RawRepresentable> : NSObject, GoogleMobileAds.FullScreenContentDelegate where E.RawValue == String, E: Hashable{
@@ -47,6 +49,7 @@ public class GADManager<E : RawRepresentable> : NSObject, GoogleMobileAds.FullSc
4749
var isLoading : [E : Bool] = [:];
4850
var isTesting : [E : Bool] = [:];
4951
var isRewardUnit : [E : Bool] = [:];
52+
var rewardedUnits : [E : Bool] = [:];
5053
var lastBeginLoading : [E : Date] = [:];
5154
var hideTestLabels : [E: Bool] = [:];
5255
var completions : [E : (E, NSObject?, Bool) -> Void] = [:];
@@ -476,10 +479,13 @@ public class GADManager<E : RawRepresentable> : NSObject, GoogleMobileAds.FullSc
476479
}else if let ad = self.adObjects[unit] as? GoogleMobileAds.RewardedAd{
477480
print("present reward ad view[\(self.window.rootViewController?.description ?? "")]");
478481
self.completions[unit] = completion;
482+
self.rewardedUnits[unit] = false;
479483
ad.present(from: viewController ?? self.window.rootViewController!) { [weak self] in
480484
guard let self = self else { return }
481485
let reward = ad.adReward
482486
print("user reward earned. type[\(reward.type)] amount[\(reward.amount)]");
487+
self.rewardedUnits[unit] = true;
488+
self.delegate?.GAD(manager: self, didEarnRewardForUnit: unit, reward: reward);
483489
}
484490
self.delegate?.GAD(manager: self, updatShownTimeForUnit: unit, showTime: Date());
485491
}
@@ -607,7 +613,9 @@ public class GADManager<E : RawRepresentable> : NSObject, GoogleMobileAds.FullSc
607613
self.delegate?.GAD(manager: self, didDismissADForUnit: unit);
608614
let completion = self.completions[unit];
609615
self.completions[unit] = nil;
610-
completion?(unit, adObj, true);
616+
let rewarded = self.isRewardUnit[unit] == true ? (self.rewardedUnits[unit] ?? false) : true;
617+
self.rewardedUnits[unit] = nil;
618+
completion?(unit, adObj, rewarded);
611619
}
612620

613621
public func ad(_ ad: GoogleMobileAds.FullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {

0 commit comments

Comments
 (0)