Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions Objective-C/BasicExample/BasicExample/ViewController.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#import "ViewController.h"

@import AVFoundation;
// [START ima_import]
@import GoogleInteractiveMediaAds;
// [END ima_import]

// [START player_variables]
@interface ViewController () <IMAAdsLoaderDelegate, IMAAdsManagerDelegate>

/// Content video player.
Expand All @@ -13,7 +16,9 @@ @interface ViewController () <IMAAdsLoaderDelegate, IMAAdsManagerDelegate>

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

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

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

@end

// [START player_setup]
@implementation ViewController

// The content URL to play.
Expand All @@ -47,11 +54,6 @@ - (void)viewDidLoad {
[self setUpContentPlayer];
}

- (IBAction)onPlayButtonTouch:(id)sender {
[self requestAds];
self.playButton.hidden = YES;
}

#pragma mark Content Player Setup

- (void)setUpContentPlayer {
Expand All @@ -66,16 +68,34 @@ - (void)setUpContentPlayer {
playerLayer.frame = self.videoView.layer.bounds;
[self.videoView.layer addSublayer:playerLayer];

// [START set_content_playhead]
// Set up our content playhead and contentComplete callback.
self.contentPlayhead = [[IMAAVPlayerContentPlayhead alloc] initWithAVPlayer:self.contentPlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contentDidFinishPlaying:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.contentPlayer.currentItem];
// [END set_content_playhead]
}

- (IBAction)onPlayButtonTouch:(id)sender {
[self requestAds];
self.playButton.hidden = YES;
}
// [END player_setup]

// [START content_complete]
- (void)contentDidFinishPlaying:(NSNotification *)notification {
// Make sure we don't call contentComplete as a result of an ad completing.
if (notification.object == self.contentPlayer.currentItem) {
[self.adsLoader contentComplete];
}
}
// [END content_complete]

#pragma mark SDK Setup

// [START request_ads]
- (void)setupAdsLoader {
self.adsLoader = [[IMAAdsLoader alloc] initWithSettings:nil];
self.adsLoader.delegate = self;
Expand All @@ -94,16 +114,11 @@ - (void)requestAds {
userContext:nil];
[self.adsLoader requestAdsWithRequest:request];
}

- (void)contentDidFinishPlaying:(NSNotification *)notification {
// Make sure we don't call contentComplete as a result of an ad completing.
if (notification.object == self.contentPlayer.currentItem) {
[self.adsLoader contentComplete];
}
}
// [END request_ads]

#pragma mark AdsLoader Delegates

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

#pragma mark AdsManager Delegates

// [START ads_manager_delegate]
- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdEvent:(IMAAdEvent *)event {
// When the SDK notified us that ads have been loaded, play them.
if (event.type == kIMAAdEvent_LOADED) {
[adsManager start];
}
}
// [END ads_manager_delegate]

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

// [START pause_resume_requests]
- (void)adsManagerDidRequestContentPause:(IMAAdsManager *)adsManager {
// The SDK is going to play ads, so pause the content.
[self.contentPlayer pause];
Expand All @@ -146,5 +167,6 @@ - (void)adsManagerDidRequestContentResume:(IMAAdsManager *)adsManager {
// The SDK is done playing ads (at least for now), so resume the content.
[self.contentPlayer play];
}
// [END pause_resume_requests]

@end
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,41 @@
// limitations under the License.
//

// [START player_import]
import AVFoundation
// [END player_import]
// [START ima_import]
import GoogleInteractiveMediaAds

// [END ima_import]

// [START player_variables]
class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMAAdsManagerDelegate {
static let contentURL = URL(
string: "https://storage.googleapis.com/gvabox/media/samples/stock.mp4")!

private var contentPlayer = AVPlayer(url: PlayerContainerViewController.contentURL)

private lazy var playerLayer: AVPlayerLayer = {
AVPlayerLayer(player: contentPlayer)
}()
// [END player_variables]

// [START ima_variables]
static let adTagURLString =
"https://pubads.g.doubleclick.net/gampad/ads?iu=/21775744923/external/"
+ "single_ad_samples&sz=640x480&cust_params=sample_ct%3Dlinear&ciu_szs=300x250%2C728x90&"
+ "gdfp_req=1&output=vast&unviewed_position_start=1&env=vp&correlator="

private let adsLoader = IMAAdsLoader()
private var adsManager: IMAAdsManager?
private var contentPlayer = AVPlayer(url: PlayerContainerViewController.contentURL)

private lazy var contentPlayhead: IMAAVPlayerContentPlayhead = {
IMAAVPlayerContentPlayhead(avPlayer: contentPlayer)
}()
// [END ima_variables]

// [START player_setup]
private lazy var videoView: UIView = {
let videoView = UIView()
videoView.translatesAutoresizingMaskIntoConstraints = false
Expand All @@ -45,14 +64,6 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA
return videoView
}()

private lazy var contentPlayhead: IMAAVPlayerContentPlayhead = {
IMAAVPlayerContentPlayhead(avPlayer: contentPlayer)
}()

private lazy var playerLayer: AVPlayerLayer = {
AVPlayerLayer(player: contentPlayer)
}()

// MARK: - View controller lifecycle methods

override func viewDidLoad() {
Expand All @@ -61,11 +72,13 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA
videoView.layer.addSublayer(playerLayer)
adsLoader.delegate = self

// [START set_content_observer]
NotificationCenter.default.addObserver(
self,
selector: #selector(contentDidFinishPlaying(_:)),
name: .AVPlayerItemDidPlayToEndTime,
object: contentPlayer.currentItem)
// [END set_content_observer]
}

override func viewDidAppear(_ animated: Bool) {
Expand All @@ -88,9 +101,11 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA
func playButtonPressed() {
requestAds()
}
// [END player_setup]

// MARK: - IMA integration methods

// [START request_ads]
private func requestAds() {
// Create ad display container for ad rendering.
let adDisplayContainer = IMAAdDisplayContainer(
Expand All @@ -104,18 +119,22 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA

adsLoader.requestAds(with: request)
}
// [END request_ads]

// MARK: - Content player methods

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

// MARK: - IMAAdsLoaderDelegate

// [START ads_loader_delegate]
func adsLoader(_ loader: IMAAdsLoader, adsLoadedWith adsLoadedData: IMAAdsLoadedData) {
// Grab the instance of the IMAAdsManager and set ourselves as the delegate.
adsManager = adsLoadedData.adsManager
Expand All @@ -135,16 +154,20 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA
}
contentPlayer.play()
}
// [END ads_loader_delegate]

// MARK: - IMAAdsManagerDelegate

// [START ads_manager_delegate]
func adsManager(_ adsManager: IMAAdsManager, didReceive event: IMAAdEvent) {
// When the SDK notifies us the ads have been loaded, play them.
if event.type == IMAAdEventType.LOADED {
adsManager.start()
}
}
// [END ads_manager_delegate]

// [START ads_manager_error]
func adsManager(_ adsManager: IMAAdsManager, didReceive error: IMAAdError) {
// Something went wrong with the ads manager after ads were loaded.
// Log the error and play the content.
Expand All @@ -153,7 +176,9 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA
}
contentPlayer.play()
}
// [END ads_manager_error]

// [START pause_resume_requests]
func adsManagerDidRequestContentPause(_ adsManager: IMAAdsManager) {
// The SDK is going to play ads, so pause the content.
contentPlayer.pause()
Expand All @@ -163,6 +188,7 @@ class PlayerContainerViewController: UIViewController, IMAAdsLoaderDelegate, IMA
// The SDK is done playing ads (at least for now), so resume the content.
contentPlayer.play()
}
// [END pause_resume_requests]

// MARK: - deinit

Expand Down
2 changes: 1 addition & 1 deletion Swift/BasicExample/Podfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '14'
platform :ios, '12'

target "BasicExample" do
pod 'GoogleAds-IMA-iOS-SDK', '~> 3.26.1'
Expand Down
Loading