Skip to content

Commit 2a58789

Browse files
JillSongcopybara-github
authored andcommitted
Add placement snippets for docs.
PiperOrigin-RevId: 796953402
1 parent 3de4e13 commit 2a58789

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 <Foundation/Foundation.h>
18+
#import <GoogleMobileAds/GoogleMobileAds.h>
19+
20+
static long adPlacementID = 2500718471;
21+
static NSString *const adUnitID = @"ca-app-pub-3940256099942544/4411468910";
22+
23+
@interface AdPlacementsSnippets : UIViewController
24+
25+
@end
26+
27+
@implementation AdPlacementsSnippets
28+
29+
// [START load_interstitial]
30+
- (void)loadInterstitial {
31+
GADRequest *request = [GADRequest request];
32+
request.placementID = adPlacementID;
33+
[GADInterstitialAd loadWithAdUnitID:adUnitID
34+
request:request
35+
completionHandler:^(GADInterstitialAd *ad, NSError *error) {
36+
if (!error) {
37+
NSLog(@"Placement ID: %lld", ad.placementID);
38+
return;
39+
}
40+
}];
41+
}
42+
// [END load_interstitial]
43+
44+
// [START show_banner]
45+
- (void)showBannerView:(GADBannerView *)bannerView {
46+
bannerView.placementID = adPlacementID;
47+
[self.view addSubview:bannerView];
48+
}
49+
// [END show_banner]
50+
51+
// [START show_interstitial]
52+
- (void)showAd:(GADInterstitialAd *)ad {
53+
ad.placementID = adPlacementID;
54+
[ad presentFromRootViewController:self];
55+
}
56+
// [END show_interstitial]
57+
58+
// [START show_native]
59+
- (void)configureView:(GADNativeAdView *)nativeAdView withNativeAd:(GADNativeAd *)nativeAd {
60+
nativeAd.placementID = adPlacementID;
61+
nativeAdView.nativeAd = nativeAd;
62+
}
63+
// [END show_native]
64+
65+
@end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 AdPlacementsSnippets: UIViewController {
20+
21+
private let adPlacementID: Int64 = 2_500_718_471
22+
private let adUnitID = "ca-app-pub-3940256099942544/6978759866"
23+
24+
// [START load_interstitial]
25+
fileprivate func loadInterstitial() async {
26+
do {
27+
let interstitial = try await InterstitialAd.load(
28+
with: adUnitID, request: Request())
29+
interstitial.placementID = adPlacementID
30+
print("Placement ID set to: \(interstitial.placementID)")
31+
} catch {
32+
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
33+
}
34+
}
35+
// [END load_interstitial]
36+
37+
// [START show_banner]
38+
private func showBanner(_ bannerView: BannerView) {
39+
bannerView.placementID = adPlacementID
40+
view.addSubview(bannerView)
41+
}
42+
// [END show_banner]
43+
44+
// [START show_interstitial]
45+
private func showInterstitial(_ ad: InterstitialAd) {
46+
ad.placementID = adPlacementID
47+
ad.present(from: self)
48+
}
49+
// [END show_interstitial]
50+
51+
// [START show_native]
52+
private func configureView(_ nativeAdView: NativeAdView, withAd nativeAd: NativeAd) {
53+
nativeAd.placementID = adPlacementID
54+
nativeAdView.nativeAd = nativeAd
55+
}
56+
// [END show_native]
57+
}

0 commit comments

Comments
 (0)