Skip to content

Commit

Permalink
Added includecode comments to interstitial Swift & Obj-C samples
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 714070350
  • Loading branch information
Justin Malandruccolo authored and copybara-github committed Jan 10, 2025
1 parent 7d9f809 commit 13d26f4
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,21 @@ - (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: %@",
[error localizedDescription]);
return;
}
self.interstitial = ad;
// [START set_the_delegate]
self.interstitial.fullScreenContentDelegate = self;
// [END set_the_delegate]
}];
// [END load_interstitial]
}

- (void)updateTimeLeft {
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)ad:(id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(NSError *)error {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)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<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}

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

@end
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)ad:(id<GADFullScreenPresentingAd>)ad
didFailToPresentFullScreenContentWithError:(NSError *)error {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}

- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)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<GADFullScreenPresentingAd>)ad {
NSLog(@"%s called", __PRETTY_FUNCTION__);
}

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

@end
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down Expand Up @@ -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")
}
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
Expand Down Expand Up @@ -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")
}
Expand All @@ -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

Expand Down

0 comments on commit 13d26f4

Please sign in to comment.