Integrate Rewarded Ads

To integrate any ad format, you must first have completed the instructions in the basic integration article.

Rewarded ads are a full screen experience where users opt in to view a video ad in exchange for something of value, such as virtual currency, in-app items, exclusive content, and more. The ad experience grants the reward after the user has watched the video for a specified duration. When that duration has been reached, your app receives a callback to grant the reward to the user.

Create a Rewarded Ad

  1. To integrate rewarded ads in your iOS app, import VungleAdsSDK and add an instance variable for VungleRewarded in your ViewController.swift file (or ViewController.h file for Objective-C).
    SwiftObjective-C
    import UIKit
    import VungleAdsSDK
    
    class LORewardedViewController: UIViewController {
        private var rewardedAd: VungleRewarded?
  2. Add the following code to your method where you want to prepare the VungleRewarded instance. To create the VungleRewarded, you will need a Placement ID.
    SwiftObjective-C
    self.rewardedAd = VungleRewarded(placementId: <YOUR_PLACEMENT_ID>)
    self.rewardedAd?.delegate = self

Note: You must set your ViewController (or any class that declares conformance to VungleRewardedDelegate) to the delegate property of the VungleRewarded instance (refer to the Register for Callbacks section below).

Load a Rewarded Ad

  1. Once you create the VungleRewarded instance, you can load an ad by calling load().
    SwiftObjective-C
    self.rewardedAd = VungleRewarded(placementId: <YOUR_PLACEMENT_ID>)
    self.rewardedAd?.delegate = self
    self.rewardedAd?.load()
  2. Your ViewController (or any class that conforms to VungleRewardedDelegate) will receive notification of load success or error through delegate callback methods (refer to the Register for Callbacks section below).

Play a Rewarded Ad

  1. Once the rewarded ad is successfully loaded, you can display it by calling present(with: <YOUR_ViewController>).
    SwiftObjective-C
    self.rewardedAd?.present(with: <YOUR_ViewController>)
  2. During the time the rewarded ad is visible to the user, you can listen for ad lifecycle events through delegate callback methods (refer to the Register for Callbacks section below).
  3. The ad is closed when the user closes it. You do not need to add steps in your code to close it.

Register for Callbacks

To receive notifications for ad events, declare conformance to VungleRewardedDelegate protocol, and implement callback methods. Each of the callback methods is optional, so you only need to implement the methods you want.

SwiftObjective-C
extension LORewardedViewController: VungleRewardedDelegate {
    // Ad load events
    func rewardedAdDidLoad(_ rewarded: VungleRewarded) {
        print("rewardedAdDidLoad")
    }
    
    func rewardedAdDidFailToLoad(_ rewarded: VungleRewarded, withError: NSError) {
        print("rewardedAdDidFailToLoad")
    }
    
    // Ad Lifecycle Events
    func rewardedAdWillPresent(_ rewarded: VungleRewarded) {
        print("rewardedAdWillPresent")
    }
    
    func rewardedAdDidPresent(_ rewarded: VungleRewarded) {
        print("rewardedAdDidPresent")
    }
    
    func rewardedAdDidFailToPresent(_ rewarded: VungleRewarded, withError: NSError) {
        print("rewardedAdDidFailToPresent")
    }
    
    func rewardedAdDidTrackImpression(_ rewarded: VungleRewarded) {
        print("rewardedAdDidTrackImpression")
    }
    
    func rewardedAdDidClick(_ rewarded: VungleRewarded) {
        print("rewardedAdDidClick")
    }
    
    func rewardedAdWillLeaveApplication(_ rewarded: VungleRewarded) {
        print("rewardedAdWillLeaveApplication")
    }
    
    func rewardedAdDidRewardUser(_ rewarded: VungleRewarded) {
        print("rewardedAdDidRewardUser")
    }
    
    func rewardedAdWillClose(_ rewarded: VungleRewarded) {
        print("rewardedAdWillClose")
    }
    
    func rewardedAdDidClose(_ rewarded: VungleRewarded) {
        print("rewardedAdDidClose")
    }
}

Test a Rewarded Ad

You can test a rewarded ad in one of two ways: by setting your app status to Test Mode so that Vungle can deliver test ads to your app, or by adding a test device to your app so that Vungle can deliver test ads specifically to the test device. Follow the instructions in Test Your Integration: Test Mode and Test Devices.

Additional Resources

Questions?

Need further assistance, feel free to reach out to us, we’re here to help!

Was this article helpful?