Skip to content

Commit 4e8c88d

Browse files
2semclaude
andauthored
docs: clarify README for new developers (#19)
- Explain Info.plist key must match enum rawValue - Add SwiftUI window initialization example - Explain needToWait parameter - Add import GoogleMobileAds note for rewarded - Add guidance on when to call prepare/show - Clarify delegate responsibility for throttling persistence Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 9378c0c commit 4e8c88d

1 file changed

Lines changed: 34 additions & 11 deletions

File tree

README.md

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ A Swift library for managing Google Mobile Ads with time-based throttling and li
1717

1818
## Setup
1919

20-
Add your ad unit IDs to `Info.plist` under the key `GADUnitIdentifiers` as a `[String: String]` dictionary:
20+
### 1. Info.plist
21+
22+
Add your ad unit IDs under the key `GADUnitIdentifiers` as a `[String: String]` dictionary. The keys must exactly match your enum's `rawValue`s.
2123

2224
```xml
2325
<key>GADUnitIdentifiers</key>
@@ -31,7 +33,7 @@ Add your ad unit IDs to `Info.plist` under the key `GADUnitIdentifiers` as a `[S
3133
</dict>
3234
```
3335

34-
Define an enum for your ad units:
36+
### 2. Define your ad units
3537

3638
```swift
3739
enum AdUnit: String {
@@ -41,38 +43,54 @@ enum AdUnit: String {
4143
}
4244
```
4345

44-
## Usage
46+
### 3. Initialize
4547

46-
### Initialization
48+
Create the manager and assign a delegate. Call this early — typically in `AppDelegate.application(_:didFinishLaunchingWithOptions:)` or your app's root scene setup.
4749

4850
```swift
51+
// UIKit
4952
let adManager = GADManager<AdUnit>(window)
5053
adManager.delegate = self
54+
55+
// SwiftUI
56+
let adManager = GADManager<AdUnit>(UIApplication.shared.connectedScenes
57+
.compactMap { ($0 as? UIWindowScene)?.keyWindow }
58+
.first!)
59+
adManager.delegate = self
5160
```
5261

62+
## Usage
63+
64+
Call `prepare` early to preload the ad. Call `show` when you want to display it.
65+
5366
### Interstitial
5467

5568
```swift
56-
// Prepare (load)
69+
// In AppDelegate or scene setup
5770
adManager.prepare(interstitialUnit: .interstitial, interval: 60 * 60)
5871

59-
// Show
72+
// When ready to show (e.g. after a level completes)
6073
adManager.show(unit: .interstitial) { unit, _, shown in
61-
// shown: true if ad was displayed and dismissed
74+
// shown: true if the ad was displayed and dismissed
6275
}
6376
```
6477

6578
### App Open
6679

80+
`needToWait: true` holds the completion until the ad finishes loading and showing, instead of returning immediately when the ad isn't ready yet.
81+
6782
```swift
6883
adManager.prepare(openingUnit: .opening)
6984

85+
// Call on every app foreground
7086
adManager.show(unit: .opening, needToWait: true) { unit, _, shown in }
7187
```
7288

7389
### Rewarded
7490

7591
```swift
92+
import GoogleMobileAds
93+
7694
adManager.prepare(rewardUnit: .rewarded)
7795

7896
adManager.show(unit: .rewarded) { unit, obj, rewarded in
@@ -94,25 +112,30 @@ if let bannerView = adManager.prepare(bannerUnit: .banner) {
94112

95113
### ATT Permission
96114

115+
Request tracking permission before loading ads to maximise ad revenue.
116+
97117
```swift
98118
if #available(iOS 14, *) {
99119
adManager.requestPermission { status in
100-
// load ads after permission resolved
120+
adManager.prepare(interstitialUnit: .interstitial)
101121
}
102122
}
103123
```
104124

105125
## Delegate
106126

127+
The delegate is responsible for persisting the last-shown time per unit so throttling works correctly across app launches.
128+
107129
```swift
108130
extension MyClass: GADManagerDelegate {
109-
// Required: persist last shown time per unit
131+
// Required: return the last time this unit was shown
110132
func GAD<E>(manager: GADManager<E>, lastShownTimeForUnit unit: E) -> Date {
111-
return UserDefaults.standard.object(forKey: unit.rawValue) as? Date ?? .distantPast
133+
return UserDefaults.standard.object(forKey: (unit as! AdUnit).rawValue) as? Date ?? .distantPast
112134
}
113135

136+
// Required: persist the shown time
114137
func GAD<E>(manager: GADManager<E>, updatShownTimeForUnit unit: E, showTime time: Date) {
115-
UserDefaults.standard.set(time, forKey: unit.rawValue)
138+
UserDefaults.standard.set(time, forKey: (unit as! AdUnit).rawValue)
116139
}
117140

118141
// Optional

0 commit comments

Comments
 (0)