Skip to content

Commit 4df3032

Browse files
malandr2copybara-github
authored andcommitted
Add includecode snippets
PiperOrigin-RevId: 770686268
1 parent e25584d commit 4df3032

File tree

6 files changed

+134
-2
lines changed

6 files changed

+134
-2
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ - (void)viewDidLoad {
7979
[super viewDidLoad];
8080

8181
self.bannerView.rootViewController = self;
82+
// [START banner_view_delegate]
8283
self.bannerView.delegate = self;
84+
// [END banner_view_delegate]
8385

8486
// Replace this ad unit ID with your own ad unit ID.
8587
self.bannerView.adUnitID = @"/21775744923/example/adaptive-banner";
@@ -141,13 +143,19 @@ - (void)loadBannerAd {
141143
// the adaptive size as outlined here - https://support.google.com/admanager/answer/9464128.
142144
// The returned ad will be centered in the ad view.
143145

146+
// [START ad_size]
144147
// Request an anchored adaptive banner with a width of 375.
145148
self.bannerView.adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(375);
149+
// [END ad_size]
150+
151+
// [START load_ad]
146152
[self.bannerView loadRequest:[GAMRequest request]];
153+
// [END load_ad]
147154
}
148155

149156
#pragma mark GADBannerViewDelegate implementation
150157

158+
// [START banner_view_delegate_methods]
151159
- (void)bannerViewDidReceiveAd:(GADBannerView *)bannerView {
152160
NSLog(@"bannerViewDidReceiveAd");
153161
}
@@ -171,6 +179,7 @@ - (void)bannerViewWillDismissScreen:(GADBannerView *)bannerView {
171179
- (void)bannerViewDidDismissScreen:(GADBannerView *)bannerView {
172180
NSLog(@"bannerViewDidDismissScreen");
173181
}
182+
// [END banner_view_delegate_methods]
174183

175184
- (void)didReceiveMemoryWarning {
176185
[super didReceiveMemoryWarning];

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ - (void)viewDidLoad {
8282
// Replace this ad unit ID with your own ad unit ID.
8383
self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2435281174";
8484
self.bannerView.rootViewController = self;
85+
// [START banner_view_delegate]
8586
self.bannerView.delegate = self;
87+
// [END banner_view_delegate]
8688

8789
__weak __typeof__(self) weakSelf = self;
8890
[GoogleMobileAdsConsentManager.sharedInstance
@@ -137,13 +139,19 @@ - (void)startGoogleMobileAdsSDK {
137139
}
138140

139141
- (void)loadBannerAd {
142+
// [START ad_size]
140143
// Request an anchored adaptive banner with a width of 375.
141144
self.bannerView.adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(375);
145+
// [END ad_size]
146+
147+
// [START load_ad]
142148
[self.bannerView loadRequest:[GADRequest request]];
149+
// [END load_ad]
143150
}
144151

145152
#pragma mark GADBannerViewDelegate implementation
146153

154+
// [START banner_view_delegate_methods]
147155
- (void)bannerViewDidReceiveAd:(GADBannerView *)bannerView {
148156
NSLog(@"bannerViewDidReceiveAd");
149157
}
@@ -167,6 +175,7 @@ - (void)bannerViewWillDismissScreen:(GADBannerView *)bannerView {
167175
- (void)bannerViewDidDismissScreen:(GADBannerView *)bannerView {
168176
NSLog(@"bannerViewDidDismissScreen");
169177
}
178+
// [END banner_view_delegate_methods]
170179

171180
- (void)didReceiveMemoryWarning {
172181
[super didReceiveMemoryWarning];
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// Copyright (C) 2025 Google, Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#import <UIKit/UIKit.h>
18+
#import <GoogleMobileAds/GoogleMobileAds.h>
19+
20+
@interface BannerSnippets : UIViewController
21+
22+
@property(nonatomic, strong) GADBannerView *bannerView;
23+
24+
@end
25+
26+
@implementation BannerSnippets
27+
28+
- (void)viewDidLoad {
29+
[super viewDidLoad];
30+
[self createBannerViewProgrammatically];
31+
}
32+
33+
- (void)createBannerViewProgrammatically {
34+
// [START create_banner_view]
35+
// Initialize the GADBannerView.
36+
self.bannerView = [[GADBannerView alloc] init];
37+
38+
self.bannerView.translatesAutoresizingMaskIntoConstraints = NO;
39+
[self.view addSubview:self.bannerView];
40+
41+
// This example doesn't give width or height constraints, as the ad size gives the banner an
42+
// intrinsic content size to size the view.
43+
[NSLayoutConstraint activateConstraints:@[
44+
// Align the banner's bottom edge with the safe area's bottom edge
45+
[self.bannerView.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor],
46+
// Center the banner horizontally in the view
47+
[self.bannerView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
48+
]];
49+
// [END create_banner_view]
50+
}
51+
52+
@end

Swift/admanager/AdManagerBannerExample/AdManagerBannerExample/ViewController.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ class ViewController: UIViewController, BannerViewDelegate {
3232

3333
bannerView.adUnitID = adUnitID
3434
bannerView.rootViewController = self
35+
// [START banner_view_delegate]
3536
bannerView.delegate = self
37+
// [END banner_view_delegate]
3638

3739
GoogleMobileAdsConsentManager.shared.gatherConsent(from: self) { [weak self] (consentError) in
3840
guard let self else { return }
@@ -119,13 +121,19 @@ class ViewController: UIViewController, BannerViewDelegate {
119121
// the adaptive size as outlined here - https://support.google.com/admanager/answer/9464128.
120122
// The returned ad will be centered in the ad view.
121123

124+
// [START ad_size]
122125
// Request an anchored adaptive banner with a width of 375.
123126
bannerView.adSize = currentOrientationAnchoredAdaptiveBanner(width: 375)
127+
// [END ad_size]
128+
129+
// [START load_ad]
124130
bannerView.load(AdManagerRequest())
131+
// [END load_ad]
125132
}
126133

127134
// MARK: - GADBannerViewDelegate methods
128135

136+
// [START banner_view_delegate_methods]
129137
func bannerViewDidReceiveAd(_ bannerView: BannerView) {
130138
print(#function)
131139
}
@@ -153,5 +161,5 @@ class ViewController: UIViewController, BannerViewDelegate {
153161
func bannerViewDidDismissScreen(_ bannerView: BannerView) {
154162
print(#function)
155163
}
156-
164+
// [END banner_view_delegate_methods]
157165
}

Swift/admob/BannerExample/BannerExample/ViewController.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class ViewController: UIViewController, BannerViewDelegate {
3131
// Replace this ad unit ID with your own ad unit ID.
3232
bannerView.adUnitID = "ca-app-pub-3940256099942544/2435281174"
3333
bannerView.rootViewController = self
34+
// [START banner_view_delegate]
3435
bannerView.delegate = self
36+
// [END banner_view_delegate]
3537

3638
GoogleMobileAdsConsentManager.shared.gatherConsent(from: self) { [weak self] consentError in
3739
guard let self else { return }
@@ -112,13 +114,19 @@ class ViewController: UIViewController, BannerViewDelegate {
112114
}
113115

114116
func loadBannerAd() {
117+
// [START ad_size]
115118
// Request an anchored adaptive banner with a width of 375.
116119
bannerView.adSize = currentOrientationAnchoredAdaptiveBanner(width: 375)
120+
// [END ad_size]
121+
122+
// [START load_ad]
117123
bannerView.load(Request())
124+
// [END load_ad]
118125
}
119126

120127
// MARK: - GADBannerViewDelegate methods
121128

129+
// [START banner_view_delegate_methods]
122130
func bannerViewDidReceiveAd(_ bannerView: BannerView) {
123131
print(#function)
124132
}
@@ -146,5 +154,5 @@ class ViewController: UIViewController, BannerViewDelegate {
146154
func bannerViewDidDismissScreen(_ bannerView: BannerView) {
147155
print(#function)
148156
}
149-
157+
// [END banner_view_delegate_methods]
150158
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Copyright (C) 2025 Google, Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
import GoogleMobileAds
18+
19+
private class BannerSnippets: UIViewController {
20+
21+
var bannerView: BannerView!
22+
23+
override func viewDidLoad() {
24+
super.viewDidLoad()
25+
createBannerViewProgrammatically()
26+
}
27+
28+
private func createBannerViewProgrammatically() {
29+
// [START create_banner_view]
30+
// Initialize the BannerView.
31+
bannerView = BannerView()
32+
33+
bannerView.translatesAutoresizingMaskIntoConstraints = false
34+
view.addSubview(bannerView)
35+
36+
// This example doesn't give width or height constraints, as the ad size gives the banner an
37+
// intrinsic content size to size the view.
38+
NSLayoutConstraint.activate([
39+
// Align the banner's bottom edge with the safe area's bottom edge
40+
bannerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
41+
// Center the banner horizontally in the view
42+
bannerView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
43+
])
44+
// [END create_banner_view]
45+
}
46+
}

0 commit comments

Comments
 (0)