Rewarded Ads
Overview
A rewarded ad is an ad that user can choose to watch in exchange for some rewards.
Create a Rewarded Ad
To create a rewarded ad, instantiate an instance of RewardedAd
with a placement Id.
- Objective C
- Swift
#import <BlueStackSDK/BlueStackSDK.h>
@interface ViewController ()
@property (strong, nonatomic) RewardedAd *rewardedAd;
@end
@implementation ViewController
- (void)viewDidLoad {
self.rewardedAd = [[RewardedAd alloc] initWithPlacementID:@"REWARDED_PLACEMENT_ID"];
}
@end
import BlueStackSDK
class ViewController: UIViewController {
var rewardedAd: RewardedAd?
override func viewDidLoad() {
super.viewDidLoad()
rewardedAd = RewardedAd(placementID: "REWARDED_PLACEMENT_ID")
}
}
Load a Rewarded Ad
Rewarded ad can be loaded by calling load(requestOptions:)
method with a RequestOptions
instance for supplying targeting information or simply calling load()
method.
- Objective C
- Swift
RequestOptions *requestOptions = [[RequestOptions alloc] initWithAge:@(25)
location:[[CLLocation alloc] initWithLatitude:48.87610 longitude:10.453]
gender:GenderMale
keyword:@"brand=myBrand;category=sport"
contentUrl:@"https://my_content_url.com/"];
[self.rewardedAd loadWithRequestOptions:requestOptions];
// Or
// [self.rewardedAd load];
let requestOptions = RequestOptions(
age: 25,
location: CLLocation.init(latitude: 48.87610, longitude: 10.453),
gender: .male,
keyword: "brand=myBrand;category=sport",
contentUrl: "https://my_content_url.com/"
)
rewardedAd?.load(requestOptions: requestOptions)
// Or
// rewardedAd?.load()
Ad Events
Registering for Rewarded Ad events
Rewarded ad provides lifecycle events and other full-screen events through RewardedAdDelegate
and FullScreenDelegate
. Use following code to listen to all the events that rewarded ad invokes.
- Objective C
- Swift
self.rewardedAd.delegate = self;
self.rewardedAd.fullScreenDelegate = self;
rewardedAd?.delegate = self
rewardedAd?.fullScreenDelegate = self
Rewarded ad lifecycle events
RewardedAdDelegate
has the following methods for listening to rewarded ad's lifecycle events.
- Objective C
- Swift
- (void)didLoadRewardedAd:(RewardedAd *)ad {
NSLog(@"Rewarded ad loaded");
}
- (void)rewardedAd:(RewardedAd *)ad didFailedToLoadWithError:(NSError *)error {
NSLog(@"Failed to load rewarded ad with error: %@", error.localizedDescription);
}
func onAdLoaded(_ ad: BlueStackSDK.RewardedAd) {
print("Rewarded ad loaded")
}
func onAdFailedToLoad(_ ad: BlueStackSDK.RewardedAd, _ error: any Error) {
print("Failed to load rewarded ad with error: \(error.localizedDescription)")
}
You can get the reward information by implementing the onRewardEarned(_ ad: , _ reward:)
delegate method.
- Objective C
- Swift
- (void)rewardedAd:(RewardedAd *)ad didEarnedReward:(Reward *)reward {
NSLog(@"Reward Earned: %f", reward.amount.floatValue);
}
func onRewardEarned(_ ad: BlueStackSDK.RewardedAd, _ reward: BlueStackSDK.Reward?) {
if let amount = reward?.amount {
print("Rewarde earned: \(amount.floatValue)")
}
}
Rewarded ad full-screen events
FullScreenDelegate
has the following methods for receiving full-screen content events.
- Objective C
- Swift
- (void)didDisplayAd:(id<FullScreenDisplayableAd>)ad {
NSLog(@"Rewarded ad displayed");
}
- (void)ad:(id<FullScreenDisplayableAd>)ad didFailedToDisplayWithError:(NSError *)error {
NSLog(@"Failed to display rewarded ad with error: %@", error.localizedDescription);
}
- (void)didClickAd:(id<FullScreenDisplayableAd>)ad {
NSLog(@"Rewarded ad clicked.");
}
- (void)didDismissAd:(id<FullScreenDisplayableAd>)ad {
NSLog(@"Rewarded ad dismissed");
}
func onAdDisplayed(_ ad: any FullScreenDisplayableAd) {
print("Rewarded ad displayed")
}
func onAdFailedToDisplay(_ ad: any FullScreenDisplayableAd, _ error: any Error) {
print("Failed to display rewarded ad with error: \(error.localizedDescription)")
}
func onAdClicked(_ ad: any FullScreenDisplayableAd) {
print("Rewarded ad clicked.")
}
func onAdDismissed(_ ad: any FullScreenDisplayableAd) {
print("Rewarded ad dismissed")
}
Show a Rewarded Ad
To show a rewarded ad call show(fromRootViewController:)
method on RewardedAd
instance with an optional root view controller.
- Objective C
- Swift
if (self.rewardedAd.isReady) {
[self.rewardedAd showFromRootViewController:self];
}
if rewardedAd.isReady
{
rewardedAd.show()
}
Destroying Rewarded Ad
You need to keep a strong reference to the RewardedAd
instance. You can release it when you are done showing the rewarded ad (Rewarded ad has dismissed) simply by setting it to nil
.
- Objective C
- Swift
self.rewardedAd = nil;
rewardedAd = nil
We discourage to create and load new rewarded ad instance in load failed callback onAdFailedToLoad(_ ad: , _ error:)
.