This page describes how to implement a VideoAdUnit
for the display of instream videos.
The VideoAdUnit is a subclass of the AdUnit class. Use the VideoAdUnit object to create and configure a video instream ad unit in your app.
Video instream is only supported with Google Ad Manager.
Create a new Video Instream Ad Unit associated with a Prebid Server configuration ID and a video size.
See AdUnit for additional parameters and methods.
VideoAdUnit(configId: String, size: CGSize(width: Int, height: Int))
Parameters
configId(String)
: Prebid Server configuration ID. Note: this is a Prebid Server impression-level stored request ID.
size(CGSize)
: Width and height of the video ad unit.
Size of video ad unit.
Parameters
width
: Width of video ad unit in DIPs.
height
: Height of video ad unit in DIPs.
placement
: [int] or [enum]: OpenRTB placement
api
: [int] or [enum]: OpenRTB api frameworks
maxBitrate
: int: OpenRTB maxBirate
minBitrate
: int: OpenRTB minBitrate
maxDuration
:int: OpenRTB maxDuration
minDuration
: int: OpenRTB minDuration
mimes
: [string]: OpenRTB mime types
playbackMethod
: [int]: OpenRTB playbackMethod
protocols
: [int] or [enum]: OpenRTB Protocols
Array of integers or enum representing the supported OpenRTB 2.5 Frameworks:
Integer representing the OpenRTB 2.5 maximum bit rate in Kbps.
Integer representing the OpenRTB 2.5 minimum bit rate in Kbps.
Integer representing the OpenRTB 2.5 maximum video ad duration in seconds.
Integer representing the OpenRTB 2.5 minimum video ad duration in seconds.
Array of strings representing the supported OpenRTB 2.5 content MIME types (e.g., “video/x-ms-wmv”, “video/mp4”).
Array of OpenRTB 2.5 playback methods. If none are specified, any method may be used. Only one method is typically used in practice. It is strongly advised to use only the first element of the array.
Array or enum of OpenRTB 2.5 supported Protocols. Values can be one of:
See our documentation on AdUnit for more details.
This utility method takes the adUnit Id of the publisher along with the adSlot sizes & Prebid custom keywords to construct the IMA adServer URL that the publisher can use to make the request.
public func constructAdTagURLForIMAWithPrebidKeys (adUnitID:String, adSlotSizes:[IMAAdSlotSize], customKeywords: [String:String]) throws -> String
var adUnit: VideoAdUnit!
var adsLoader: IMAAdsLoader!
var adsManager: IMAAdsManager!
//setup PB Video
let adUnit = VideoAdUnit(configId: "1001-1")
//video parameters
parameters.mimes = ["video/mp4"]
parameters.protocols = [2,3,7] // or alternative enum values [Protocols.VAST_2_0, Protocols.VAST_3_0]
parameters.playbackMethod = [1] // or alternative enum value [PlaybackMethod.AutoPlaySoundOn]
parameters.api = [1,2] // or alternative enum values [Api.VPAID_1, Api.VPAID_2]
parameters.maxBitrate = 1500
parameters.minBitrate = 300
parameters.maxDuration = 30
parameters.minDuration = 5
adUnit.parameters = parameters
//setup IMA Video
adsLoader = IMAAdsLoader(settings: nil)
adsLoader.delegate = self
adUnit.fetchDemand { (ResultCode, prebidKeys: [String : String]?) in
print("prebid keys")
if(ResultCode == .prebidDemandFetchSuccess){
do {
let adServerTag:String = try IMAUtils.shared.constructAdTagURLForIMAWithPrebidKeys(adUnitID: "your_ad_unit",
adSlotSizes: [.Size640x480,.Size400x300], customKeywords: prebidKeys!)
let adDisplayContainer = IMAAdDisplayContainer(adContainer: self.appInstreamView)
// Create an ad request with our ad tag, display container, and optional user context.
let request = IMAAdsRequest(adTagUrl: adServerTag, adDisplayContainer: adDisplayContainer, contentPlayhead: nil, userContext: nil)
self.adsLoader.requestAds(with: request)
}catch {
print (error)
}
} else {
print ("Error constructing IMA Tag")
}
}
}
//adsLoader delegate
func adsLoader(_ loader: IMAAdsLoader!, adsLoadedWith adsLoadedData: IMAAdsLoadedData!) {
// Grab the instance of the IMAAdsManager and set ourselves as the delegate.
adsManager = adsLoadedData.adsManager
adsManager.delegate = self
// Create ads rendering settings and tell the SDK to use the in-app browser.
let adsRenderingSettings = IMAAdsRenderingSettings()
adsRenderingSettings.webOpenerPresentingController = self
// Initialize the ads manager.
adsManager.initialize(with: adsRenderingSettings)
}
func adsLoader(_ loader: IMAAdsLoader!, failedWith adErrorData: IMAAdLoadingErrorData!) {
print("Error loading ads: \(adErrorData.adError.message ?? "nil")")
}
//adsManager delegate
func adsManager(_ adsManager: IMAAdsManager!, didReceive event: IMAAdEvent!) {
if event.type == IMAAdEventType.LOADED {
// When the SDK notifies us that ads have been loaded, play them.
adsManager.start()
}
}
func adsManager(_ adsManager: IMAAdsManager!, didReceive error: IMAAdError!) {
print("AdsManager error: \(error.message ?? "nil")")
}