From c3b47a2b6357013ba8fac2eb4a71e343fb704180 Mon Sep 17 00:00:00 2001 From: Justin Malandruccolo Date: Fri, 10 Jan 2025 09:00:49 -0800 Subject: [PATCH] Added includecode comments to interstitial Swift & Obj-C samples PiperOrigin-RevId: 714070350 --- .github/workflows/build.yml | 6 +- .../ViewController.m | 67 +++++++++++++------ .../InterstitialExample/ViewController.m | 64 ++++++++++++------ .../ViewController.swift | 37 ++++++++-- .../InterstitialExample/ViewController.swift | 36 ++++++++-- 5 files changed, 154 insertions(+), 56 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bb829ea4..50dd4af3 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,11 +19,11 @@ jobs: matrix: include: - { regex: "Objective-C/advanced", xcode_version: "15.3" } - - { regex: "Swift/advanced", xcode_version: "16.0" } + - { regex: "Swift/advanced", xcode_version: "16.1" } - { regex: "Objective-C/admob", xcode_version: "15.3" } - - { regex: "Swift/admob", xcode_version: "16.0" } + - { regex: "Swift/admob", xcode_version: "16.1" } - { regex: "Objective-C/admanager", xcode_version: "15.3" } - - { regex: "Swift/admanager", xcode_version: "16.0" } + - { regex: "Swift/admanager", xcode_version: "16.1" } steps: - name: Clone Repo uses: actions/checkout@v1 diff --git a/Objective-C/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.m b/Objective-C/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.m index 695b4fa4..1ab4fdd9 100644 --- a/Objective-C/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.m +++ b/Objective-C/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.m @@ -142,9 +142,9 @@ - (void)startNewGame { } - (void)loadInterstitial { - GAMRequest *request = [GAMRequest request]; + // [START load_interstitial] [GAMInterstitialAd loadWithAdManagerAdUnitID:@"/21775744923/example/interstitial" - request:request + request:[GAMRequest request] completionHandler:^(GAMInterstitialAd *ad, NSError *error) { if (error) { NSLog(@"Failed to load interstitial ad with error: %@", @@ -152,8 +152,11 @@ - (void)loadInterstitial { return; } self.interstitial = ad; + // [START set_the_delegate] self.interstitial.fullScreenContentDelegate = self; + // [END set_the_delegate] }]; + // [END load_interstitial] } - (void)updateTimeLeft { @@ -209,24 +212,28 @@ - (void)endGame { [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { - ViewController *strongSelf = weakSelf; + __strong typeof(self) strongSelf = weakSelf; if (!strongSelf) { - return; - } - if (strongSelf.interstitial && - [strongSelf.interstitial - canPresentFromRootViewController:strongSelf - error:nil]) { - [strongSelf.interstitial presentFromRootViewController:strongSelf]; - } else { - NSLog(@"Ad wasn't ready"); - } - strongSelf.playAgainButton.hidden = NO; - }]; + return; + } + [strongSelf presentInterstitialAd]; + strongSelf.playAgainButton.hidden = NO; + }]; [alert addAction:alertAction]; [self presentViewController:alert animated:YES completion:nil]; } +- (void)presentInterstitialAd { + if (!self.interstitial || + ![self.interstitial canPresentFromRootViewController:self error:nil]) { + NSLog(@"Ad wasn't ready"); + return; + } + // [START present_interstitial] + [self.interstitial presentFromRootViewController:self]; + // [END present_interstitial] +} + - (IBAction)privacySettingsTapped:(UIBarButtonItem *)sender { [self pauseGame]; @@ -287,16 +294,34 @@ - (IBAction)playAgain:(id)sender { } #pragma GADFullScreenContentdelegate implementation -- (void)adWillPresentFullScreenContent:(id)ad { - NSLog(@"Ad will present full screen content."); + +// [START ad_events] +- (void)adDidRecordImpression:(id)ad { + NSLog(@"%s called", __PRETTY_FUNCTION__); +} + +- (void)adDidRecordClick:(id)ad { + NSLog(@"%s called", __PRETTY_FUNCTION__); +} + +- (void)ad:(id)ad + didFailToPresentFullScreenContentWithError:(NSError *)error { + NSLog(@"%s called", __PRETTY_FUNCTION__); +} + +- (void)adWillPresentFullScreenContent:(id)ad { + NSLog(@"%s called", __PRETTY_FUNCTION__); } -- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error { - NSLog(@"Ad failed to present full screen content with error %@.", [error localizedDescription]); +- (void)adWillDismissFullScreenContent:(id)ad { + NSLog(@"%s called", __PRETTY_FUNCTION__); } -- (void)adDidDismissFullScreenContent:(id)ad { - NSLog(@"Ad did dismiss full screen content."); +- (void)adDidDismissFullScreenContent:(id)ad { + NSLog(@"%s called", __PRETTY_FUNCTION__); + // Clear the interstitial ad. + self.interstitial = nil; } +// [END ad_events] @end diff --git a/Objective-C/admob/InterstitialExample/InterstitialExample/ViewController.m b/Objective-C/admob/InterstitialExample/InterstitialExample/ViewController.m index e23f2278..f2b32a88 100644 --- a/Objective-C/admob/InterstitialExample/InterstitialExample/ViewController.m +++ b/Objective-C/admob/InterstitialExample/InterstitialExample/ViewController.m @@ -142,18 +142,21 @@ - (void)startNewGame { } - (void)loadInterstitial { - GADRequest *request = [GADRequest request]; + // [START load_interstitial] [GADInterstitialAd loadWithAdUnitID:@"ca-app-pub-3940256099942544/4411468910" - request:request + request:[GADRequest request] completionHandler:^(GADInterstitialAd *ad, NSError *error) { if (error) { NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]); return; } self.interstitial = ad; + // [START set_the_delegate] self.interstitial.fullScreenContentDelegate = self; + // [END set_the_delegate] }]; + // [END load_interstitial] } - (void)updateTimeLeft { @@ -209,24 +212,28 @@ - (void)endGame { [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { - ViewController *strongSelf = weakSelf; + __strong typeof(self) strongSelf = weakSelf; if (!strongSelf) { - return; - } - if (strongSelf.interstitial && - [strongSelf.interstitial - canPresentFromRootViewController:strongSelf - error:nil]) { - [strongSelf.interstitial presentFromRootViewController:strongSelf]; - } else { - NSLog(@"Ad wasn't ready"); - } - strongSelf.playAgainButton.hidden = NO; + return; + } + [strongSelf presentInterstitialAd]; + strongSelf.playAgainButton.hidden = NO; }]; [alert addAction:alertAction]; [self presentViewController:alert animated:YES completion:nil]; } +- (void)presentInterstitialAd { + if (!self.interstitial || + ![self.interstitial canPresentFromRootViewController:self error:nil]) { + NSLog(@"Ad wasn't ready"); + return; + } + // [START present_interstitial] + [self.interstitial presentFromRootViewController:self]; + // [END present_interstitial] +} + #pragma Interstitial button actions - (IBAction)privacySettingsTapped:(UIBarButtonItem *)sender { @@ -290,16 +297,33 @@ - (IBAction)playAgain:(id)sender { #pragma GADFullScreeContentDelegate implementation -- (void)adWillPresentFullScreenContent:(id)ad { - NSLog(@"Ad will present full screen content."); +// [START ad_events] +- (void)adDidRecordImpression:(id)ad { + NSLog(@"%s called", __PRETTY_FUNCTION__); +} + +- (void)adDidRecordClick:(id)ad { + NSLog(@"%s called", __PRETTY_FUNCTION__); +} + +- (void)ad:(id)ad + didFailToPresentFullScreenContentWithError:(NSError *)error { + NSLog(@"%s called", __PRETTY_FUNCTION__); +} + +- (void)adWillPresentFullScreenContent:(id)ad { + NSLog(@"%s called", __PRETTY_FUNCTION__); } -- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error { - NSLog(@"Ad failed to present full screen content with error %@.", [error localizedDescription]); +- (void)adWillDismissFullScreenContent:(id)ad { + NSLog(@"%s called", __PRETTY_FUNCTION__); } -- (void)adDidDismissFullScreenContent:(id)ad { - NSLog(@"Ad did dismiss full screen content."); +- (void)adDidDismissFullScreenContent:(id)ad { + NSLog(@"%s called", __PRETTY_FUNCTION__); + // Clear the interstitial ad. + self.interstitial = nil; } +// [END ad_events] @end diff --git a/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.swift b/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.swift index f04a59b2..331ec656 100644 --- a/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.swift +++ b/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.swift @@ -189,15 +189,19 @@ class ViewController: UIViewController, @preconcurrency GADFullScreenContentDele repeats: true) } + // [START load_interstitial] fileprivate func loadInterstitial() async { do { interstitial = try await GAMInterstitialAd.load( withAdManagerAdUnitID: "/21775744923/example/interstitial", request: GAMRequest()) + // [START set_the_delegate] interstitial?.fullScreenContentDelegate = self + // [END set_the_delegate] } catch { print("Failed to load interstitial ad with error: \(error.localizedDescription)") } } + // [END load_interstitial] fileprivate func updateTimeLeft() { gameText.text = "\(timeLeft) seconds left!" @@ -252,7 +256,9 @@ class ViewController: UIViewController, @preconcurrency GADFullScreenContentDele style: .cancel, handler: { [weak self] action in if let ad = self?.interstitial { - ad.present(fromRootViewController: self!) + // [START present_interstitial] + ad.present(fromRootViewController: self) + // [END present_interstitial] } else { print("Ad wasn't ready") } @@ -275,18 +281,37 @@ class ViewController: UIViewController, @preconcurrency GADFullScreenContentDele } // MARK: - GADFullScreenContentDelegate + + // [START ad_events] + func adDidRecordImpression(_ ad: GADFullScreenPresentingAd) { + print("\(#function) called") + } + + func adDidRecordClick(_ ad: GADFullScreenPresentingAd) { + print("\(#function) called") + } + + func ad( + _ ad: GADFullScreenPresentingAd, + didFailToPresentFullScreenContentWithError error: Error + ) { + print("\(#function) called") + } + func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) { - print("Ad will present full screen content.") + print("\(#function) called") } - func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) - { - print("Ad failed to present full screen content with error \(error.localizedDescription).") + func adWillDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) { + print("\(#function) called") } func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) { - print("Ad did dismiss full screen content.") + print("\(#function) called") + // Clear the interstitial ad. + interstitial = nil } + // [END ad_events] // MARK: - deinit diff --git a/Swift/admob/InterstitialExample/InterstitialExample/ViewController.swift b/Swift/admob/InterstitialExample/InterstitialExample/ViewController.swift index dbc8d3f2..62cfebb7 100644 --- a/Swift/admob/InterstitialExample/InterstitialExample/ViewController.swift +++ b/Swift/admob/InterstitialExample/InterstitialExample/ViewController.swift @@ -189,15 +189,19 @@ class ViewController: UIViewController, @preconcurrency GADFullScreenContentDele repeats: true) } + // [START load_interstitial] fileprivate func loadInterstitial() async { do { interstitial = try await GADInterstitialAd.load( withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: GADRequest()) + // [START set_the_delegate] interstitial?.fullScreenContentDelegate = self + // [END set_the_delegate] } catch { print("Failed to load interstitial ad with error: \(error.localizedDescription)") } } + // [END load_interstitial] fileprivate func updateTimeLeft() { gameText.text = "\(timeLeft) seconds left!" @@ -252,7 +256,9 @@ class ViewController: UIViewController, @preconcurrency GADFullScreenContentDele style: .cancel, handler: { [weak self] action in if let ad = self?.interstitial { - ad.present(fromRootViewController: self!) + // [START present_interstitial] + ad.present(fromRootViewController: self) + // [END present_interstitial] } else { print("Ad wasn't ready") } @@ -276,18 +282,36 @@ class ViewController: UIViewController, @preconcurrency GADFullScreenContentDele // MARK: - GADFullScreenContentDelegate + // [START ad_events] + func adDidRecordImpression(_ ad: GADFullScreenPresentingAd) { + print("\(#function) called") + } + + func adDidRecordClick(_ ad: GADFullScreenPresentingAd) { + print("\(#function) called") + } + + func ad( + _ ad: GADFullScreenPresentingAd, + didFailToPresentFullScreenContentWithError error: Error + ) { + print("\(#function) called") + } + func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) { - print("Ad will present full screen content.") + print("\(#function) called") } - func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) - { - print("Ad failed to present full screen content with error \(error.localizedDescription).") + func adWillDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) { + print("\(#function) called") } func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) { - print("Ad did dismiss full screen content.") + print("\(#function) called") + // Clear the interstitial ad. + interstitial = nil } + // [END ad_events] // MARK: - deinit