Skip to content

iOS SDK - Rewarded ads

Rewarded ads are full-screen ads that give the user an in-app reward, such as coins or an extra life, for watching the ad to the end. Users choose to watch them, so offer them in exchange for something the user wants.

Before you start, install and initialize the SDK.

1. Get the rewarded ad object

Get the ad object for your ad unit ID and set its delegate. The SDK caches one instance per ad unit, so calling getRewardedAd again with the same ID returns the same object.

private var rewarded: RewardedAd?

override func viewDidLoad() {
    super.viewDidLoad()

    rewarded = A11.shared.getRewardedAd(adUnitID: "adUnitId")
    rewarded?.delegate = self
}

2. Implement the delegate

Set the delegate before calling load(on:), so you don't miss any events. When an ad closes or fails to show, load the next one. When loading fails, we recommend retrying with exponential backoff. See the full example.

extension MyViewController: RewardedAdDelegate {
    func onRewardedAdLoaded(adInfo: AdInfo) {}
    func onRewardedAdLoadFailed(error: AdLoadError) {}
    func onRewardedAdShown(adInfo: AdInfo) {}
    func onRewardedAdShowFailed(adInfo: AdInfo?, error: AdShowError) {}
    func onRewardedAdClicked(adInfo: AdInfo) {}
    func onRewardedAdRewardEarned(adInfo: AdInfo) {
        // Grant the reward here, see step 5
    }
    func onRewardedAdClosed(adInfo: AdInfo) {}
    func onRewardedAdRevenue(adInfo: AdInfo) {}
}

3. Load the ad

Load the ad well before you need it. When onRewardedAdLoaded fires, offer the reward to the user.

func loadRewarded() {
    rewarded?.load(on: self)
}

4. Show the ad

Call show(on:) only when the user chooses to watch, for example when they tap "Watch ad for a reward".

rewarded?.show(on: self)

5. Grant the reward

Grant the reward in onRewardedAdRewardEarned.

func onRewardedAdRewardEarned(adInfo: AdInfo) {
    grantReward() // Your own reward logic, for example add coins
}

Caution

onRewardedAdClosed fires whether or not the user earned the reward. The reward should be granted only in onRewardedAdRewardEarned.

Full example

class MyViewController: UIViewController {

    private var rewarded: RewardedAd?
    private var isRewardedReady = false
    private var retryDelay: TimeInterval = 5

    override func viewDidLoad() {
        super.viewDidLoad()

        rewarded = A11.shared.getRewardedAd(adUnitID: "adUnitId")
        rewarded?.delegate = self
        loadRewarded()
    }

    func loadRewarded() {
        rewarded?.load(on: self)
    }

    // Call when the user chooses to watch, for example taps "Watch ad for a reward"
    func showRewarded() {
        guard isRewardedReady else { return }
        isRewardedReady = false
        rewarded?.show(on: self)
    }
}

extension MyViewController: RewardedAdDelegate {
    func onRewardedAdLoaded(adInfo: AdInfo) {
        isRewardedReady = true // Offer the reward to the user
        retryDelay = 5
    }

    func onRewardedAdLoadFailed(error: AdLoadError) {
        print("Rewarded load failed: \(error)")
        // Retry with exponential backoff: 5 s, 10 s, 20 s and so on, up to 60 s
        DispatchQueue.main.asyncAfter(deadline: .now() + retryDelay) { [weak self] in
            self?.loadRewarded()
        }
        retryDelay = min(retryDelay * 2, 60)
    }

    func onRewardedAdShowFailed(adInfo: AdInfo?, error: AdShowError) {
        print("Rewarded show failed: \(error)")
        loadRewarded()
    }

    func onRewardedAdRewardEarned(adInfo: AdInfo) {
        // Grant the reward here
    }

    func onRewardedAdClosed(adInfo: AdInfo) {
        // Preload the next one
        loadRewarded()
    }

    func onRewardedAdShown(adInfo: AdInfo) {}
    func onRewardedAdClicked(adInfo: AdInfo) {}
    func onRewardedAdRevenue(adInfo: AdInfo) {}
}