Skip to content

Commit 44586dd

Browse files
google-ima-devrel-botIMA Developer Relations
authored andcommitted
Adds documentation region tags.
PiperOrigin-RevId: 776201852
1 parent 627fd83 commit 44586dd

File tree

3 files changed

+75
-26
lines changed

3 files changed

+75
-26
lines changed

Objective-C/BasicExample/BasicExample/ViewController.m

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
// [START player_imports]
12
#import "ViewController.h"
23

34
@import AVFoundation;
5+
// [END player_imports]
6+
// [START ima_import]
47
@import GoogleInteractiveMediaAds;
8+
// [END ima_import]
59

10+
// [START player_variables]
611
@interface ViewController () <IMAAdsLoaderDelegate, IMAAdsManagerDelegate>
712

813
/// Content video player.
@@ -13,7 +18,9 @@ @interface ViewController () <IMAAdsLoaderDelegate, IMAAdsManagerDelegate>
1318

1419
/// UIView in which we will render our AVPlayer for content.
1520
@property(nonatomic, weak) IBOutlet UIView *videoView;
21+
// [END player_variables]
1622

23+
// [START ima_variables]
1724
// SDK
1825
/// Entry point for the SDK. Used to make ad requests.
1926
@property(nonatomic, strong) IMAAdsLoader *adsLoader;
@@ -23,9 +30,11 @@ @interface ViewController () <IMAAdsLoaderDelegate, IMAAdsManagerDelegate>
2330

2431
/// Main point of interaction with the SDK. Created by the SDK as the result of an ad request.
2532
@property(nonatomic, strong) IMAAdsManager *adsManager;
33+
// [END ima_variables]
2634

2735
@end
2836

37+
// [START player_setup]
2938
@implementation ViewController
3039

3140
// The content URL to play.
@@ -47,11 +56,6 @@ - (void)viewDidLoad {
4756
[self setUpContentPlayer];
4857
}
4958

50-
- (IBAction)onPlayButtonTouch:(id)sender {
51-
[self requestAds];
52-
self.playButton.hidden = YES;
53-
}
54-
5559
#pragma mark Content Player Setup
5660

5761
- (void)setUpContentPlayer {
@@ -66,16 +70,34 @@ - (void)setUpContentPlayer {
6670
playerLayer.frame = self.videoView.layer.bounds;
6771
[self.videoView.layer addSublayer:playerLayer];
6872

73+
// [START set_content_playhead]
6974
// Set up our content playhead and contentComplete callback.
7075
self.contentPlayhead = [[IMAAVPlayerContentPlayhead alloc] initWithAVPlayer:self.contentPlayer];
7176
[[NSNotificationCenter defaultCenter] addObserver:self
7277
selector:@selector(contentDidFinishPlaying:)
7378
name:AVPlayerItemDidPlayToEndTimeNotification
7479
object:self.contentPlayer.currentItem];
80+
// [END set_content_playhead]
81+
}
82+
83+
- (IBAction)onPlayButtonTouch:(id)sender {
84+
[self requestAds];
85+
self.playButton.hidden = YES;
86+
}
87+
// [END player_setup]
88+
89+
// [START content_complete]
90+
- (void)contentDidFinishPlaying:(NSNotification *)notification {
91+
// Make sure we don't call contentComplete as a result of an ad completing.
92+
if (notification.object == self.contentPlayer.currentItem) {
93+
[self.adsLoader contentComplete];
94+
}
7595
}
96+
// [END content_complete]
7697

7798
#pragma mark SDK Setup
7899

100+
// [START request_ads]
79101
- (void)setupAdsLoader {
80102
self.adsLoader = [[IMAAdsLoader alloc] initWithSettings:nil];
81103
self.adsLoader.delegate = self;
@@ -94,16 +116,11 @@ - (void)requestAds {
94116
userContext:nil];
95117
[self.adsLoader requestAdsWithRequest:request];
96118
}
97-
98-
- (void)contentDidFinishPlaying:(NSNotification *)notification {
99-
// Make sure we don't call contentComplete as a result of an ad completing.
100-
if (notification.object == self.contentPlayer.currentItem) {
101-
[self.adsLoader contentComplete];
102-
}
103-
}
119+
// [END request_ads]
104120

105121
#pragma mark AdsLoader Delegates
106122

123+
// [START ads_loader_delegate]
107124
- (void)adsLoader:(IMAAdsLoader *)loader adsLoadedWithData:(IMAAdsLoadedData *)adsLoadedData {
108125
// Grab the instance of the IMAAdsManager and set ourselves as the delegate.
109126
self.adsManager = adsLoadedData.adsManager;
@@ -120,23 +137,29 @@ - (void)adsLoader:(IMAAdsLoader *)loader failedWithErrorData:(IMAAdLoadingErrorD
120137
NSLog(@"Error loading ads: %@", adErrorData.adError.message);
121138
[self.contentPlayer play];
122139
}
140+
// [END ads_loader_delegate]
123141

124142
#pragma mark AdsManager Delegates
125143

144+
// [START ads_manager_delegate]
126145
- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdEvent:(IMAAdEvent *)event {
127146
// When the SDK notified us that ads have been loaded, play them.
128147
if (event.type == kIMAAdEvent_LOADED) {
129148
[adsManager start];
130149
}
131150
}
151+
// [END ads_manager_delegate]
132152

153+
// [START ads_manager_error]
133154
- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdError:(IMAAdError *)error {
134155
// Something went wrong with the ads manager after ads were loaded. Log the error and play the
135156
// content.
136157
NSLog(@"AdsManager error: %@", error.message);
137158
[self.contentPlayer play];
138159
}
160+
// [END ads_manager_error]
139161

162+
// [START pause_resume_requests]
140163
- (void)adsManagerDidRequestContentPause:(IMAAdsManager *)adsManager {
141164
// The SDK is going to play ads, so pause the content.
142165
[self.contentPlayer pause];
@@ -146,5 +169,6 @@ - (void)adsManagerDidRequestContentResume:(IMAAdsManager *)adsManager {
146169
// The SDK is done playing ads (at least for now), so resume the content.
147170
[self.contentPlayer play];
148171
}
172+
// [END pause_resume_requests]
149173

150174
@end

Swift/BasicExample/BasicExample/PlayerContainerViewController.swift

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,32 @@
1414
// limitations under the License.
1515
//
1616

17+
// [START imports]
1718
import AVFoundation
1819
import GoogleInteractiveMediaAds
1920

21+
// [END imports]
22+
23+
// [START player_variables]
2024
class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMAAdsManagerDelegate {
21-
static let contentURL = URL(
22-
string: "https://storage.googleapis.com/gvabox/media/samples/stock.mp4")!
2325

24-
static let adTagURLString =
25-
"https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/"
26-
+ "single_ad_samples&sz=640x480&cust_params=sample_ct%3Dlinear&ciu_szs=300x250%2C728x90&"
27-
+ "gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator="
26+
private var contentPlayer = AVPlayer(url: PlayerContainerViewController.contentURL)
2827

28+
private lazy var playerLayer: AVPlayerLayer = {
29+
AVPlayerLayer(player: contentPlayer)
30+
}()
31+
// [END player_variables]
32+
33+
// [START ima_variables]
2934
private let adsLoader = IMAAdsLoader()
3035
private var adsManager: IMAAdsManager?
31-
private var contentPlayer = AVPlayer(url: PlayerContainerViewController.contentURL)
3236

37+
private lazy var contentPlayhead: IMAAVPlayerContentPlayhead = {
38+
IMAAVPlayerContentPlayhead(avPlayer: contentPlayer)
39+
}()
40+
// [END ima_variables]
41+
42+
// [START player_setup]
3343
private lazy var videoView: UIView = {
3444
let videoView = UIView()
3545
videoView.translatesAutoresizingMaskIntoConstraints = false
@@ -45,13 +55,13 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA
4555
return videoView
4656
}()
4757

48-
private lazy var contentPlayhead: IMAAVPlayerContentPlayhead = {
49-
IMAAVPlayerContentPlayhead(avPlayer: contentPlayer)
50-
}()
58+
static let contentURL = URL(
59+
string: "https://storage.googleapis.com/gvabox/media/samples/stock.mp4")!
5160

52-
private lazy var playerLayer: AVPlayerLayer = {
53-
AVPlayerLayer(player: contentPlayer)
54-
}()
61+
static let adTagURLString =
62+
"https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/"
63+
+ "single_ad_samples&sz=640x480&cust_params=sample_ct%3Dlinear&ciu_szs=300x250%2C728x90&"
64+
+ "gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator="
5565

5666
// MARK: - View controller lifecycle methods
5767

@@ -61,11 +71,13 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA
6171
videoView.layer.addSublayer(playerLayer)
6272
adsLoader.delegate = self
6373

74+
// [START set_content_observer]
6475
NotificationCenter.default.addObserver(
6576
self,
6677
selector: #selector(contentDidFinishPlaying(_:)),
6778
name: .AVPlayerItemDidPlayToEndTime,
6879
object: contentPlayer.currentItem)
80+
// [END set_content_observer]
6981
}
7082

7183
override func viewDidAppear(_ animated: Bool) {
@@ -88,9 +100,11 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA
88100
func playButtonPressed() {
89101
requestAds()
90102
}
103+
// [END player_setup]
91104

92105
// MARK: - IMA integration methods
93106

107+
// [START request_ads]
94108
private func requestAds() {
95109
// Create ad display container for ad rendering.
96110
let adDisplayContainer = IMAAdDisplayContainer(
@@ -104,18 +118,22 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA
104118

105119
adsLoader.requestAds(with: request)
106120
}
121+
// [END request_ads]
107122

108123
// MARK: - Content player methods
109124

125+
// [START content_complete]
110126
@objc func contentDidFinishPlaying(_ notification: Notification) {
111127
// Make sure we don't call contentComplete as a result of an ad completing.
112128
if notification.object as? AVPlayerItem == contentPlayer.currentItem {
113129
adsLoader.contentComplete()
114130
}
115131
}
132+
// [END content_complete]
116133

117134
// MARK: - IMAAdsLoaderDelegate
118135

136+
// [START ads_loader_delegate]
119137
func adsLoader(_ loader: IMAAdsLoader, adsLoadedWith adsLoadedData: IMAAdsLoadedData) {
120138
// Grab the instance of the IMAAdsManager and set ourselves as the delegate.
121139
adsManager = adsLoadedData.adsManager
@@ -135,16 +153,20 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA
135153
}
136154
contentPlayer.play()
137155
}
156+
// [END ads_loader_delegate]
138157

139158
// MARK: - IMAAdsManagerDelegate
140159

160+
// [START ads_manager_delegate]
141161
func adsManager(_ adsManager: IMAAdsManager, didReceive event: IMAAdEvent) {
142162
// When the SDK notifies us the ads have been loaded, play them.
143163
if event.type == IMAAdEventType.LOADED {
144164
adsManager.start()
145165
}
146166
}
167+
// [END ads_manager_delegate]
147168

169+
// [START ads_manager_error]
148170
func adsManager(_ adsManager: IMAAdsManager, didReceive error: IMAAdError) {
149171
// Something went wrong with the ads manager after ads were loaded.
150172
// Log the error and play the content.
@@ -153,7 +175,9 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA
153175
}
154176
contentPlayer.play()
155177
}
178+
// [END ads_manager_error]
156179

180+
// [START pause_resume_requests]
157181
func adsManagerDidRequestContentPause(_ adsManager: IMAAdsManager) {
158182
// The SDK is going to play ads, so pause the content.
159183
contentPlayer.pause()
@@ -163,6 +187,7 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA
163187
// The SDK is done playing ads (at least for now), so resume the content.
164188
contentPlayer.play()
165189
}
190+
// [END pause_resume_requests]
166191

167192
// MARK: - deinit
168193

Swift/BasicExample/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
source 'https://github.com/CocoaPods/Specs.git'
22

3-
platform :ios, '14'
3+
platform :ios, '12'
44

55
target "BasicExample" do
66
pod 'GoogleAds-IMA-iOS-SDK', '~> 3.26.1'

0 commit comments

Comments
 (0)