|
| 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 | +// Replace this ad unit ID with your own ad unit ID. |
| 23 | +static NSString *const nativeAdUnitID = @"ca-app-pub-3940256099942544/3986624511"; |
| 24 | + |
| 25 | +@interface NativeAdOptionsSnippets : UIViewController <GADNativeAdDelegate> |
| 26 | +// The ad loader. |
| 27 | +@property(nonatomic, strong) GADAdLoader *adLoader; |
| 28 | +@end |
| 29 | + |
| 30 | +@implementation NativeAdOptionsSnippets |
| 31 | + |
| 32 | +- (void)setMediaAspectRatio { |
| 33 | + // [START set_media_aspect_ratio] |
| 34 | + GADNativeAdMediaAdLoaderOptions *nativeOptions = [[GADNativeAdMediaAdLoaderOptions alloc] init]; |
| 35 | + nativeOptions.mediaAspectRatio = GADMediaAspectRatioAny; |
| 36 | + |
| 37 | + self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:nativeAdUnitID |
| 38 | + rootViewController:self |
| 39 | + adTypes:@[ GADAdLoaderAdTypeNative ] |
| 40 | + options:@[ nativeOptions ]]; |
| 41 | + // [END set_media_aspect_ratio] |
| 42 | +} |
| 43 | + |
| 44 | +- (void)setDisableImageLoading { |
| 45 | + // [START set_disable_image_loading] |
| 46 | + GADNativeAdImageAdLoaderOptions *nativeOptions = [[GADNativeAdImageAdLoaderOptions alloc] init]; |
| 47 | + nativeOptions.disableImageLoading = YES; |
| 48 | + |
| 49 | + self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:nativeAdUnitID |
| 50 | + rootViewController:self |
| 51 | + adTypes:@[ GADAdLoaderAdTypeNative ] |
| 52 | + options:@[ nativeOptions ]]; |
| 53 | + // [END set_disable_image_loading] |
| 54 | +} |
| 55 | + |
| 56 | +- (void)setRequestMultipleImages { |
| 57 | + // [START set_request_multiple_images] |
| 58 | + GADNativeAdImageAdLoaderOptions *nativeOptions = [[GADNativeAdImageAdLoaderOptions alloc] init]; |
| 59 | + nativeOptions.shouldRequestMultipleImages = YES; |
| 60 | + |
| 61 | + self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:nativeAdUnitID |
| 62 | + rootViewController:self |
| 63 | + adTypes:@[ GADAdLoaderAdTypeNative ] |
| 64 | + options:@[ nativeOptions ]]; |
| 65 | + // [END set_request_multiple_images] |
| 66 | +} |
| 67 | + |
| 68 | +- (void)setAdChoicesPosition { |
| 69 | + // [START set_ad_choices_position] |
| 70 | + GADNativeAdViewAdOptions *nativeOptions = [[GADNativeAdViewAdOptions alloc] init]; |
| 71 | + nativeOptions.preferredAdChoicesPosition = GADAdChoicesPositionTopRightCorner; |
| 72 | + |
| 73 | + self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:nativeAdUnitID |
| 74 | + rootViewController:self |
| 75 | + adTypes:@[ GADAdLoaderAdTypeNative ] |
| 76 | + options:@[ nativeOptions ]]; |
| 77 | + // [END set_ad_choices_position] |
| 78 | +} |
| 79 | + |
| 80 | +// [START create_ad_choices_view] |
| 81 | +- (void)createAdChoicesViewWithNativeAdView:(GADNativeAdView *)nativeAdView { |
| 82 | + // Define a custom position for the AdChoices icon. |
| 83 | + CGRect customRect = CGRectMake(100, 100, 15, 15); |
| 84 | + GADAdChoicesView *customAdChoicesView = [[GADAdChoicesView alloc] initWithFrame:customRect]; |
| 85 | + [nativeAdView addSubview:customAdChoicesView]; |
| 86 | + nativeAdView.adChoicesView = customAdChoicesView; |
| 87 | +} |
| 88 | +// [END create_ad_choices_view] |
| 89 | + |
| 90 | +- (void)setMuted { |
| 91 | + // [START set_muted] |
| 92 | + GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init]; |
| 93 | + videoOptions.startMuted = NO; |
| 94 | + |
| 95 | + self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:nativeAdUnitID |
| 96 | + rootViewController:self |
| 97 | + adTypes:@[ GADAdLoaderAdTypeNative ] |
| 98 | + options:@[ videoOptions ]]; |
| 99 | + // [END set_muted] |
| 100 | +} |
| 101 | + |
| 102 | +- (void)setRequestCustomPlaybackControls { |
| 103 | + // [START set_request_custom_playback_controls] |
| 104 | + GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init]; |
| 105 | + videoOptions.customControlsRequested = YES; |
| 106 | + |
| 107 | + self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:nativeAdUnitID |
| 108 | + rootViewController:self |
| 109 | + adTypes:@[ GADAdLoaderAdTypeNative ] |
| 110 | + options:@[ videoOptions ]]; |
| 111 | + // [END set_request_custom_playback_controls] |
| 112 | +} |
| 113 | + |
| 114 | +// [START check_custom_controls_enabled] |
| 115 | +- (BOOL)checkCustomControlsEnabledWithNativeAd:(GADNativeAd *)nativeAd { |
| 116 | + GADVideoController *videoController = nativeAd.mediaContent.videoController; |
| 117 | + return videoController.customControlsEnabled; |
| 118 | +} |
| 119 | +// [END check_custom_controls_enabled] |
| 120 | + |
| 121 | +- (void)setCustomSwipeGesture { |
| 122 | + // [START set_custom_swipe_gesture] |
| 123 | + GADNativeAdCustomClickGestureOptions *swipeGestureOptions = |
| 124 | + [[GADNativeAdCustomClickGestureOptions alloc] |
| 125 | + initWithSwipeGestureDirection:UISwipeGestureRecognizerDirectionRight |
| 126 | + tapsAllowed:YES]; |
| 127 | + |
| 128 | + self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:nativeAdUnitID |
| 129 | + rootViewController:self |
| 130 | + adTypes:@[ GADAdLoaderAdTypeNative ] |
| 131 | + options:@[ swipeGestureOptions ]]; |
| 132 | + // [END set_custom_swipe_gesture] |
| 133 | +} |
| 134 | + |
| 135 | +#pragma mark - GADNativeAdDelegate |
| 136 | + |
| 137 | +// [START custom_swipe_gesture_delegate] |
| 138 | +// Called when a swipe gesture click is recorded, as configured in |
| 139 | +// GADNativeAdCustomClickGestureOptions. |
| 140 | +- (void)nativeAdDidRecordSwipeGestureClick:(GADNativeAd *)nativeAd { |
| 141 | + NSLog(@"A swipe gesture click has occurred."); |
| 142 | +} |
| 143 | + |
| 144 | +// Called when a swipe gesture click or a tap click is recorded. |
| 145 | +- (void)nativeAdDidRecordClick:(GADNativeAd *)nativeAd { |
| 146 | + NSLog(@"A swipe gesture click or tap click has occurred."); |
| 147 | +} |
| 148 | +// [END custom_swipe_gesture_delegate] |
| 149 | + |
| 150 | +NS_ASSUME_NONNULL_END |
| 151 | + |
| 152 | +@end |
0 commit comments