Skip to content

Commit 8eb52b6

Browse files
JillSongcopybara-github
authored andcommitted
Added region tags for native advanced snippets.
PiperOrigin-RevId: 800470845
1 parent d54f9ef commit 8eb52b6

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,11 @@ - (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(NSError *)
194194

195195
#pragma mark GADNativeAdLoaderDelegate implementation
196196

197+
// [START display_native_ad]
197198
- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativeAd {
199+
// [START_EXCLUDE]
198200
self.refreshButton.enabled = YES;
201+
// [END_EXCLUDE]
199202

200203
GADNativeAdView *nativeAdView = self.nativeAdView;
201204

@@ -205,7 +208,9 @@ - (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativ
205208
// Populate the native ad view with the native ad assets.
206209
// The headline and mediaContent are guaranteed to be present in every native ad.
207210
((UILabel *)nativeAdView.headlineView).text = nativeAd.headline;
211+
// [START set_media_content]
208212
nativeAdView.mediaView.mediaContent = nativeAd.mediaContent;
213+
// [END set_media_content]
209214

210215
// This app uses a fixed width for the GADMediaView and changes its height
211216
// to match the aspect ratio of the media content it displays.
@@ -265,6 +270,7 @@ - (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativ
265270
// Note: this should always be done after populating the ad views.
266271
nativeAdView.nativeAd = nativeAd;
267272
}
273+
// [END display_native_ad]
268274

269275
#pragma mark GADVideoControllerDelegate implementation
270276

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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/GoogleMobileAds.h>
18+
#import <UIKit/UIKit.h>
19+
20+
NS_ASSUME_NONNULL_BEGIN
21+
22+
@interface NativeAdSnippets : NSObject
23+
@end
24+
25+
@implementation NativeAdSnippets
26+
27+
- (void)setContentModeForNativeAdView:(GADNativeAdView *)nativeAdView nativeAd:(GADNativeAd *)ad {
28+
// [START set_content_mode]
29+
GADMediaView *mediaView = nativeAdView.mediaView;
30+
if (mediaView) {
31+
mediaView.contentMode = UIViewContentModeScaleAspectFit;
32+
}
33+
// [END set_content_mode]
34+
}
35+
36+
NS_ASSUME_NONNULL_END
37+
38+
@end

Swift/admob/NativeAdvancedExample/NativeAdvancedExample/ViewController.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,21 @@ extension ViewController: @preconcurrency AdLoaderDelegate {
200200

201201
extension ViewController: @preconcurrency NativeAdLoaderDelegate {
202202

203+
// [START display_native_ad]
203204
func adLoader(_ adLoader: AdLoader, didReceive nativeAd: NativeAd) {
205+
// [START_EXCLUDE]
204206
refreshAdButton.isEnabled = true
207+
// [END_EXCLUDE]
205208

206209
// Set ourselves as the native ad delegate to be notified of native ad events.
207210
nativeAd.delegate = self
208211

209212
// Populate the native ad view with the native ad assets.
210213
// The headline and mediaContent are guaranteed to be present in every native ad.
211214
(nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline
215+
// [START set_media_content]
212216
nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent
217+
// [END set_media_content]
213218

214219
// Some native ads will include a video asset, while others do not. Apps can use the
215220
// GADVideoController's hasVideoContent property to determine if one is present, and adjust their
@@ -270,6 +275,7 @@ extension ViewController: @preconcurrency NativeAdLoaderDelegate {
270275
// Note: this should always be done after populating the ad views.
271276
nativeAdView.nativeAd = nativeAd
272277
}
278+
// [END display_native_ad]
273279
}
274280

275281
// MARK: - GADNativeAdDelegate implementation
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
class NativeAdSnippets {
20+
21+
private func setContentMode(_ nativeAdView: NativeAdView, for ad: NativeAd) {
22+
// [START set_content_mode]
23+
nativeAdView.mediaView?.contentMode = .scaleAspectFit
24+
// [END set_content_mode]
25+
}
26+
27+
}

0 commit comments

Comments
 (0)