Skip to content

Commit 8aae221

Browse files
malandr2copybara-github
authored andcommitted
Added includecode comments to interstitial Swift & Obj-C samples
PiperOrigin-RevId: 714070350
1 parent d24f305 commit 8aae221

File tree

4 files changed

+151
-49
lines changed

4 files changed

+151
-49
lines changed

Objective-C/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.m

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,21 @@ - (void)startNewGame {
142142
}
143143

144144
- (void)loadInterstitial {
145-
GAMRequest *request = [GAMRequest request];
145+
// [START load_interstitial]
146146
[GAMInterstitialAd loadWithAdManagerAdUnitID:@"/21775744923/example/interstitial"
147-
request:request
147+
request:[GAMRequest request]
148148
completionHandler:^(GAMInterstitialAd *ad, NSError *error) {
149149
if (error) {
150150
NSLog(@"Failed to load interstitial ad with error: %@",
151151
[error localizedDescription]);
152152
return;
153153
}
154154
self.interstitial = ad;
155+
// [START set_the_delegate]
155156
self.interstitial.fullScreenContentDelegate = self;
157+
// [END set_the_delegate]
156158
}];
159+
// [END load_interstitial]
157160
}
158161

159162
- (void)updateTimeLeft {
@@ -209,24 +212,28 @@ - (void)endGame {
209212
[UIAlertAction actionWithTitle:@"OK"
210213
style:UIAlertActionStyleCancel
211214
handler:^(UIAlertAction *action) {
212-
ViewController *strongSelf = weakSelf;
215+
__strong __typeof__(self) strongSelf = weakSelf;
213216
if (!strongSelf) {
214-
return;
215-
}
216-
if (strongSelf.interstitial &&
217-
[strongSelf.interstitial
218-
canPresentFromRootViewController:strongSelf
219-
error:nil]) {
220-
[strongSelf.interstitial presentFromRootViewController:strongSelf];
221-
} else {
222-
NSLog(@"Ad wasn't ready");
223-
}
224-
strongSelf.playAgainButton.hidden = NO;
225-
}];
217+
return;
218+
}
219+
[strongSelf presentInterstitialAd];
220+
strongSelf.playAgainButton.hidden = NO;
221+
}];
226222
[alert addAction:alertAction];
227223
[self presentViewController:alert animated:YES completion:nil];
228224
}
229225

226+
- (void)presentInterstitialAd {
227+
if (!self.interstitial ||
228+
![self.interstitial canPresentFromRootViewController:self error:nil]) {
229+
NSLog(@"Ad wasn't ready");
230+
return;
231+
}
232+
// [START present_interstitial]
233+
[self.interstitial presentFromRootViewController:self];
234+
// [END present_interstitial]
235+
}
236+
230237
- (IBAction)privacySettingsTapped:(UIBarButtonItem *)sender {
231238
[self pauseGame];
232239

@@ -287,16 +294,36 @@ - (IBAction)playAgain:(id)sender {
287294
}
288295

289296
#pragma GADFullScreenContentdelegate implementation
290-
- (void)adWillPresentFullScreenContent:(id)ad {
291-
NSLog(@"Ad will present full screen content.");
297+
298+
// [START ad_events]
299+
- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad {
300+
NSLog(@"%s called", __PRETTY_FUNCTION__);
301+
}
302+
303+
- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
304+
NSLog(@"%s called", __PRETTY_FUNCTION__);
305+
}
306+
307+
- (void)ad:(id<GADFullScreenPresentingAd>)ad
308+
didFailToPresentFullScreenContentWithError:(NSError *)error {
309+
NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
310+
// Clear the interstitial ad.
311+
self.interstitial = nil;
312+
}
313+
314+
- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
315+
NSLog(@"%s called", __PRETTY_FUNCTION__);
292316
}
293317

294-
- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
295-
NSLog(@"Ad failed to present full screen content with error %@.", [error localizedDescription]);
318+
- (void)adWillDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
319+
NSLog(@"%s called", __PRETTY_FUNCTION__);
296320
}
297321

298-
- (void)adDidDismissFullScreenContent:(id)ad {
299-
NSLog(@"Ad did dismiss full screen content.");
322+
- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
323+
NSLog(@"%s called", __PRETTY_FUNCTION__);
324+
// Clear the interstitial ad.
325+
self.interstitial = nil;
300326
}
327+
// [END ad_events]
301328

302329
@end

Objective-C/admob/InterstitialExample/InterstitialExample/ViewController.m

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,21 @@ - (void)startNewGame {
142142
}
143143

144144
- (void)loadInterstitial {
145-
GADRequest *request = [GADRequest request];
145+
// [START load_interstitial]
146146
[GADInterstitialAd
147147
loadWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910"
148-
request:request
148+
request:[GADRequest request]
149149
completionHandler:^(GADInterstitialAd *ad, NSError *error) {
150150
if (error) {
151151
NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]);
152152
return;
153153
}
154154
self.interstitial = ad;
155+
// [START set_the_delegate]
155156
self.interstitial.fullScreenContentDelegate = self;
157+
// [END set_the_delegate]
156158
}];
159+
// [END load_interstitial]
157160
}
158161

159162
- (void)updateTimeLeft {
@@ -209,24 +212,28 @@ - (void)endGame {
209212
[UIAlertAction actionWithTitle:@"OK"
210213
style:UIAlertActionStyleCancel
211214
handler:^(UIAlertAction *action) {
212-
ViewController *strongSelf = weakSelf;
215+
__strong __typeof__(self) strongSelf = weakSelf;
213216
if (!strongSelf) {
214-
return;
215-
}
216-
if (strongSelf.interstitial &&
217-
[strongSelf.interstitial
218-
canPresentFromRootViewController:strongSelf
219-
error:nil]) {
220-
[strongSelf.interstitial presentFromRootViewController:strongSelf];
221-
} else {
222-
NSLog(@"Ad wasn't ready");
223-
}
224-
strongSelf.playAgainButton.hidden = NO;
217+
return;
218+
}
219+
[strongSelf presentInterstitialAd];
220+
strongSelf.playAgainButton.hidden = NO;
225221
}];
226222
[alert addAction:alertAction];
227223
[self presentViewController:alert animated:YES completion:nil];
228224
}
229225

226+
- (void)presentInterstitialAd {
227+
if (!self.interstitial ||
228+
![self.interstitial canPresentFromRootViewController:self error:nil]) {
229+
NSLog(@"Ad wasn't ready");
230+
return;
231+
}
232+
// [START present_interstitial]
233+
[self.interstitial presentFromRootViewController:self];
234+
// [END present_interstitial]
235+
}
236+
230237
#pragma Interstitial button actions
231238

232239
- (IBAction)privacySettingsTapped:(UIBarButtonItem *)sender {
@@ -290,16 +297,35 @@ - (IBAction)playAgain:(id)sender {
290297

291298
#pragma GADFullScreeContentDelegate implementation
292299

293-
- (void)adWillPresentFullScreenContent:(id)ad {
294-
NSLog(@"Ad will present full screen content.");
300+
// [START ad_events]
301+
- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad {
302+
NSLog(@"%s called", __PRETTY_FUNCTION__);
303+
}
304+
305+
- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
306+
NSLog(@"%s called", __PRETTY_FUNCTION__);
307+
}
308+
309+
- (void)ad:(id<GADFullScreenPresentingAd>)ad
310+
didFailToPresentFullScreenContentWithError:(NSError *)error {
311+
NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription);
312+
// Clear the interstitial ad.
313+
self.interstitial = nil;
314+
}
315+
316+
- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
317+
NSLog(@"%s called", __PRETTY_FUNCTION__);
295318
}
296319

297-
- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
298-
NSLog(@"Ad failed to present full screen content with error %@.", [error localizedDescription]);
320+
- (void)adWillDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
321+
NSLog(@"%s called", __PRETTY_FUNCTION__);
299322
}
300323

301-
- (void)adDidDismissFullScreenContent:(id)ad {
302-
NSLog(@"Ad did dismiss full screen content.");
324+
- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
325+
NSLog(@"%s called", __PRETTY_FUNCTION__);
326+
// Clear the interstitial ad.
327+
self.interstitial = nil;
303328
}
329+
// [END ad_events]
304330

305331
@end

Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.swift

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,19 @@ class ViewController: UIViewController, FullScreenContentDelegate {
189189
repeats: true)
190190
}
191191

192+
// [START load_interstitial]
192193
fileprivate func loadInterstitial() async {
193194
do {
194195
interstitial = try await AdManagerInterstitialAd.load(
195196
with: "/21775744923/example/interstitial", request: AdManagerRequest())
197+
// [START set_the_delegate]
196198
interstitial?.fullScreenContentDelegate = self
199+
// [END set_the_delegate]
197200
} catch {
198201
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
199202
}
200203
}
204+
// [END load_interstitial]
201205

202206
fileprivate func updateTimeLeft() {
203207
gameText.text = "\(timeLeft) seconds left!"
@@ -252,7 +256,9 @@ class ViewController: UIViewController, FullScreenContentDelegate {
252256
style: .cancel,
253257
handler: { [weak self] action in
254258
if let ad = self?.interstitial {
259+
// [START present_interstitial]
255260
ad.present(from: self!)
261+
// [END present_interstitial]
256262
} else {
257263
print("Ad wasn't ready")
258264
}
@@ -275,17 +281,36 @@ class ViewController: UIViewController, FullScreenContentDelegate {
275281
}
276282

277283
// MARK: - GADFullScreenContentDelegate
278-
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
279-
print("Ad will present full screen content.")
284+
285+
// [START ad_events]
286+
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
287+
print("\(#function) called")
288+
}
289+
290+
func adDidRecordClick(_ ad: FullScreenPresentingAd) {
291+
print("\(#function) called")
280292
}
281293

282294
func ad(_ ad: FullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
283-
print("Ad failed to present full screen content with error \(error.localizedDescription).")
295+
print("\(#function) called with error: \(error.localizedDescription)")
296+
// Clear the interstitial ad.
297+
interstitial = nil
298+
}
299+
300+
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
301+
print("\(#function) called")
302+
}
303+
304+
func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
305+
print("\(#function) called")
284306
}
285307

286308
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
287-
print("Ad did dismiss full screen content.")
309+
print("\(#function) called")
310+
// Clear the interstitial ad.
311+
interstitial = nil
288312
}
313+
// [END ad_events]
289314

290315
// MARK: - deinit
291316

Swift/admob/InterstitialExample/InterstitialExample/ViewController.swift

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,19 @@ class ViewController: UIViewController, FullScreenContentDelegate {
189189
repeats: true)
190190
}
191191

192+
// [START load_interstitial]
192193
fileprivate func loadInterstitial() async {
193194
do {
194195
interstitial = try await InterstitialAd.load(
195196
with: "ca-app-pub-3940256099942544/4411468910", request: Request())
197+
// [START set_the_delegate]
196198
interstitial?.fullScreenContentDelegate = self
199+
// [END set_the_delegate]
197200
} catch {
198201
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
199202
}
200203
}
204+
// [END load_interstitial]
201205

202206
fileprivate func updateTimeLeft() {
203207
gameText.text = "\(timeLeft) seconds left!"
@@ -252,7 +256,9 @@ class ViewController: UIViewController, FullScreenContentDelegate {
252256
style: .cancel,
253257
handler: { [weak self] action in
254258
if let ad = self?.interstitial {
259+
// [START present_interstitial]
255260
ad.present(from: self!)
261+
// [END present_interstitial]
256262
} else {
257263
print("Ad wasn't ready")
258264
}
@@ -276,17 +282,35 @@ class ViewController: UIViewController, FullScreenContentDelegate {
276282

277283
// MARK: - GADFullScreenContentDelegate
278284

279-
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
280-
print("Ad will present full screen content.")
285+
// [START ad_events]
286+
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
287+
print("\(#function) called")
288+
}
289+
290+
func adDidRecordClick(_ ad: FullScreenPresentingAd) {
291+
print("\(#function) called")
281292
}
282293

283294
func ad(_ ad: FullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
284-
print("Ad failed to present full screen content with error \(error.localizedDescription).")
295+
print("\(#function) called with error: \(error.localizedDescription)")
296+
// Clear the interstitial ad.
297+
interstitial = nil
298+
}
299+
300+
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
301+
print("\(#function) called")
302+
}
303+
304+
func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
305+
print("\(#function) called")
285306
}
286307

287308
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
288-
print("Ad did dismiss full screen content.")
309+
print("\(#function) called")
310+
// Clear the interstitial ad.
311+
interstitial = nil
289312
}
313+
// [END ad_events]
290314

291315
// MARK: - deinit
292316

0 commit comments

Comments
 (0)