Rewarded
Warning
This is the documentation for the Advanced API. Most developers should use our Simplified API, which comes with several built-in features such as:
-
automatic ad loading
-
retries for failed requests
See the documentation for the Simplified API here:
Rewarded ads (also known as rewarded video ads) are a fullscreen format, but unlike interstitials, the user is incentivized to watch its entire duration (usually 30 seconds) in order to get an in-app reward, such as in-game currency, extra lives or hints to pass a level.
The didEarnReward
callback will be triggered by the adapted network, signaling that you can give the user their reward. If the user decides to skip the ad, this callback will not be called.
Create an Rewarded instance
Register ad callbacks
// Assign a delegate to handle the ad callbacks
rewarded.delegate = self
// [...]
// Load Callbacks
func didCompletePrebidding(result: PrebiddingResults) {
print("Rewarded client-side bidding finished!")
}
func didLoad(result: LoadResult) {
print("Rewarded loaded!")
}
func failedToLoad(result: LoadResult?, error: LoadError) {
print("Rewarded failed to load. Reason: \(error.localizedDescription)")
}
// Present Callbacks
func didPresent() {
print("Rewarded is being presented!")
}
func failedToPresent(error: PresentError) {
// If you need to resume your app's flow, make sure to do it here and in the didDismiss callback
print("Rewarded failed to present. Reason: \(error.localizedDescription)")
}
func willDismiss() {
print("Rewarded will be dismissed!")
}
func didDismiss() {
// If you need to resume your app's flow, make sure to do it here and in the failedToPresent callback
print("Rewarded was dismissed!")
}
func didClick() {
print("Rewarded was clicked!")
}
func didRecordImpression(data: ImpressionData) {
print("Rewarded impression, with revenue: \(data.revenue)")
}
// Reward Callback
func didEarnReward() {
print("Rewarded ad earned a reward!")
}
Load an ad
Load an ad (with custom properties)
var properties = CustomProperties()
properties.addString(key: "game_mode", value: "classic")
rewarded.load(customProperties: properties)
Present an ad
if rewarded.isReady {
rewarded.present(fromViewController: self, fromAdSpace: "rewarded-ad-space")
}
Code example
RewardedViewController.swift
import UIKit
import XMediator
class RewardedViewController: UIViewController {
private var rewarded: Rewarded?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func loadButtonTouchUpInside(_ sender: Any) {
let rewarded = Rewarded.create(placementId: "<placement-id>")
rewarded.delegate = self
rewarded.load()
self.rewarded = rewarded
}
@IBAction func presentButtonTouchUpInside(_ sender: Any) {
if let rewarded = rewarded, rewarded.isReady {
rewarded.present(fromViewController: self)
}
}
}
extension RewardedViewController: RewardedDelegate {
// Load Callbacks
func didCompletePrebidding(result: PrebiddingResults) {
print("Rewarded client-side bidding finished!")
}
func didLoad(result: LoadResult) {
print("Rewarded loaded!")
}
func failedToLoad(result: LoadResult?, error: LoadError) {
print("Rewarded failed to load. Reason: \(error.localizedDescription)")
}
// Present Callbacks
func didPresent() {
print("Rewarded is being presented!")
}
func failedToPresent(error: PresentError) {
// If you need to resume your app's flow, make sure to do it here and in the didDismiss callback
print("Rewarded failed to present. Reason: \(error.localizedDescription)")
}
func willDismiss() {
print("Rewarded will be dismissed!")
}
func didDismiss() {
// If you need to resume your app's flow, make sure to do it here and in the failedToPresent callback
print("Rewarded was dismissed!")
}
func didClick() {
print("Rewarded was clicked!")
}
func didRecordImpression(data: ImpressionData) {
print("Rewarded impression, with revenue: \(data.revenue)")
}
// Reward Callback
func didEarnReward() {
}
}