Rewarded Ads
Integrate CloudX rewarded ads in iOS apps
@interface YourViewController () <CLXRewardedDelegate, CLXAdRevenueDelegate>
@property (nonatomic, strong) CLXRewarded *rewardedAd;
@property (nonatomic, assign) NSInteger retryAttempt;
@end
@implementation YourViewController
- (void)createRewardedAd {
self.rewardedAd = [[CloudXCore shared] createRewardedWithAdUnitId:@"your-rewarded-ad-unit-id"];
self.rewardedAd.delegate = self;
self.rewardedAd.revenueDelegate = self;
[self.rewardedAd load];
}
- (void)showRewardedAd {
if (self.rewardedAd.isReady) {
// Basic show
[self.rewardedAd showFromViewController:self];
// Or with optional placement and custom data for tracking
// [self.rewardedAd showFromViewController:self placement:@"bonus_coins" customData:@"level:5,coins:100"];
} else {
NSLog(@"Rewarded ad not ready");
}
}
- (void)dealloc {
[self.rewardedAd destroy];
}
#pragma mark - CLXRewardedDelegate
- (void)didLoadAd:(CLXAd *)ad {
NSLog(@"Rewarded ad loaded from %@", ad.networkName);
self.retryAttempt = 0;
}
- (void)didFailToLoadAd:(NSString *)adUnitId error:(CLXError *)error {
NSLog(@"Rewarded ad failed to load: %@", error.localizedDescription);
self.retryAttempt++;
NSTimeInterval delay = pow(2.0, MIN(5, self.retryAttempt));
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.rewardedAd load];
});
}
- (void)didDisplayAd:(CLXAd *)ad {
NSLog(@"Rewarded ad displayed");
}
- (void)didFailToDisplayAd:(CLXAd *)ad error:(CLXError *)error {
NSLog(@"Rewarded ad failed to display: %@", error.localizedDescription);
[self.rewardedAd load];
}
- (void)didHideAd:(CLXAd *)ad {
NSLog(@"Rewarded ad hidden");
[self.rewardedAd load]; // Reload for next use
}
- (void)didClickAd:(CLXAd *)ad {
NSLog(@"Rewarded ad clicked");
}
- (void)didRewardUserForAd:(CLXAd *)ad withReward:(CLXReward *)reward {
NSLog(@"User rewarded: %ld %@", (long)reward.amount, reward.label);
// Grant the reward to the user
}
#pragma mark - CLXAdRevenueDelegate
- (void)didPayRevenueForAd:(CLXAd *)ad {
NSLog(@"Rewarded revenue: %@ from %@", ad.revenue, ad.networkName);
}
@endclass YourViewController: UIViewController, CLXRewardedDelegate, CLXAdRevenueDelegate {
private var rewardedAd: CLXRewarded?
private var retryAttempt = 0
func createRewardedAd() {
rewardedAd = CloudXCore.shared.createRewarded(adUnitId: "your-rewarded-ad-unit-id")
rewardedAd?.delegate = self
rewardedAd?.revenueDelegate = self
rewardedAd?.load()
}
func showRewardedAd() {
if rewardedAd?.isReady == true {
// Basic show
rewardedAd?.show(from: self)
// Or with optional placement and custom data for tracking
// rewardedAd?.show(from: self, placement: "bonus_coins", customData: "level:5,coins:100")
} else {
print("Rewarded ad not ready")
}
}
deinit {
rewardedAd?.destroy()
}
// MARK: - CLXRewardedDelegate
func didLoad(_ ad: CLXAd) {
print("Rewarded ad loaded from \(ad.networkName ?? "unknown")")
retryAttempt = 0
}
func didFailToLoadAd(_ adUnitId: String, error: CLXError) {
print("Rewarded ad failed to load: \(error.localizedDescription)")
retryAttempt += 1
let delay = pow(2.0, Double(min(5, retryAttempt)))
DispatchQueue.main.asyncAfter(deadline: .now() + delay) { [weak self] in
self?.rewardedAd?.load()
}
}
func didDisplay(_ ad: CLXAd) {
print("Rewarded ad displayed")
}
func didFailToDisplay(_ ad: CLXAd, error: CLXError) {
print("Rewarded ad failed to display: \(error.localizedDescription)")
rewardedAd?.load()
}
func didHide(_ ad: CLXAd) {
print("Rewarded ad hidden")
rewardedAd?.load() // Reload for next use
}
func didClick(_ ad: CLXAd) {
print("Rewarded ad clicked")
}
func didRewardUser(for ad: CLXAd, with reward: CLXReward) {
print("User rewarded: \(reward.amount) \(reward.label)")
// Grant the reward to the user
}
// MARK: - CLXAdRevenueDelegate
func didPayRevenue(for ad: CLXAd) {
print("Rewarded revenue: \(ad.revenue ?? 0) from \(ad.networkName ?? "unknown")")
}
}Ad readiness
isReady is true from the moment an ad loads until you show it or destroy the ad object. An ad network can stop being able to show an ad without telling the SDK. isReady stays true in that case, and the problem is found when you call showFromViewController: (Swift show(from:)). If the network does report that its ad has expired, the ad is no longer ready.
- If you show an ad the network can no longer show,
didFailToDisplayAd:error:(SwiftdidFailToDisplay(_:error:)) fires and the ad is no longer ready. Callloadto get a new ad. - In that case, if CloudX has enabled show recovery for your app and ad unit, the SDK loads a replacement and shows it instead. You receive the replacement’s display and revenue callbacks, and no
didFailToDisplayAd:error:. If an expired-ad replacement is already loading, the SDK uses it, anddidReloadExpiredAd:withNewAd:(SwiftdidReloadExpiredAd(_:with:)) fires before it is shown. If no replacement loads, or none is ready within the time limit CloudX sets,didFailToDisplayAd:error:fires. - If you call
loadwhile an ad is ready, no new request starts.didLoadAd:(SwiftdidLoad(_:)) fires at once with that ad. - A
loadcall while an ad is showing runs after the ad closes. Aloadcall while a show waits for a recovery replacement also runs after the show ends, and receives exactly onedidLoadAd:ordidFailToLoadAd:error:.
Expired ads
A loaded ad that is not shown before it expires is reloaded automatically on the same ad object. The current ad stays ready and can still be shown until the replacement is ready.
- To be told when a replacement is ready, set an
expirationDelegate. A replacement that loads on its own does not calldidLoadAd:(SwiftdidLoad(_:)) again. - If you show the ad while the replacement loads, the replacement is kept. If it is ready when the ad closes, or no
loadcomes before it is ready, it becomes ready on the ad object and theexpirationDelegateis called then. If you callloadafter the ad closes while the replacement is still loading, thatloadruns its own request and getsdidLoadAd:, and noexpirationDelegatecall follows for that replacement. - If you call
loadwhile the expired ad is still loaded, no new request starts.didLoadAd:fires at once with the expired ad. The replacement keeps loading, anddidReloadExpiredAd:withNewAd:(SwiftdidReloadExpiredAd(_:with:)) is called when it is ready. - If the replacement fails to load while the expired ad is still loaded,
didFailToLoadAd:error:(SwiftdidFailToLoadAd(_:error:)) fires and the ad is no longer ready. Reload and its retries are set per ad unit by CloudX, and retries are off by default, so one no-fill ends here. This is the normal outcome, not an integration error: callloadagain after a delay rather than in a tight loop.
// Add CLXAdExpirationDelegate to the protocols YourViewController adopts
self.rewardedAd.expirationDelegate = self;
- (void)didReloadExpiredAd:(CLXAd *)expiredAd withNewAd:(CLXAd *)newAd {
NSLog(@"Expired rewarded ad replaced by an ad from %@", newAd.networkName);
}// Add CLXAdExpirationDelegate to the protocols YourViewController adopts
rewardedAd?.expirationDelegate = self
func didReloadExpiredAd(_ expiredAd: CLXAd, with newAd: CLXAd) {
print("Expired rewarded ad replaced by an ad from \(newAd.networkName ?? "unknown")")
}