Rewarded Ads - cleveradssolutions/CAS-iOS GitHub Wiki
⚡ Before you start
Make sure you have correctly configure user consent flow.
This guide explains how to integrate rewarded video ads into an iOS app.
Note
By default, the auto-load ads mode is used and a load method does not need to be called.
Call manual load ad before each show if you use CASLoadingManagerMode.manual
only.
Manual load Rewarded ads.
Swift
manager.loadRewardedAd()
Objective-C
[manager loadRewardedAd];
You can get a callback for the successful loading of the ads:
Swift
class AnyViewController: UIViewController, CASLoadDelegate{
override func viewDidLoad() {
super.viewDidLoad()
manager.adLoadDelegate = self
}
func onAdLoaded(_ adType: CASType){
if (adType == .rewarded){
// Interstitial loaded
}
}
func onAdFailedToLoad(_ adType: CASType, withError error: String?){
if (adType == .rewarded){
// Interstitial failed to load with error
}
}
}
Objective-C
@interface ViewController : UIViewController>CASLoadDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
manager.adLoadDelegate = self; // Weak reference
}
- (void)onAdLoaded:(enum CASType)adType {
if (adType == CASTypeRewarded) {
// Interstitial loaded
}
}
- (void)onAdFailedToLoad:(enum CASType) adType withError:(NSString *)error {
if (adType == CASTypeRewarded) {
// Interstitial failed to load with error
}
}
@end
You can ask for the ad availability directly by calling the following function:
Swift
let adLoaded = manager.isRewardedAdReady
Objective-C
BOOL adLoaded = manager.isRewardedAdReady;
The CASCallback
handles events related to displaying your Interstitial ad.
Swift
class AnyViewController: UIViewController, CASCallback {
func willShown(ad adStatus: CASStatusHandler){
// Ad will present content
}
func didShowAdFailed(error: String){
// Ad did fail to present content.
}
func didClickedAd(){
// Ad did click content
}
func didCompletedAd(){
// Ad did complete content
}
func didClosedAd(){
// Ad did dismiss content.
}
}
Objective-C
- (void)willShownWithAd:(id>CASStatusHandler>)adStatus {
// Ad will present content
}
- (void)didShowAdFailedWithError:(_NSString_ *)error {
// Ad did fail to present content.
}
- (void)didClickedAd {
// Ad did click content
}
- (void)didCompletedAd {
// Ad did complete content
}
- (void)didClosedAd {
// Ad did dismiss content.
}
Warning
When an error occurs during ad impression, executed the didShowAdFailed(:)
only.
didClosedAd()
in this case will not be executed, since the impression is not considered successful.
Note
Read more about event willShown(ad:)
with Impression level data.
Before displaying a rewarded ad to users, you must present the user with an explicit choice to view rewarded ad content in exchange for a reward. Rewarded ads must always be an opt-in experience.
The presentRewardedAd()
method requires UIViewController
and CASCallback instances as arguments.
-
controller
The controller from which the Rewarded ad should be shown. -
callback
The callback for Rewarded ad events. The reference to thecallback
is weak and may be removed from memory.
When presenting your ad, you must provide a CASCallback
object to handle the reward for the user didCompletedAd
.
Swift
manager.presentRewardedAd(fromRootViewController: self, callback: self)
Objective-C
[manager presentRewardedAdFromRootViewController:self callback:self];
Indicates if the application’s audio is muted. Affects initial mute state for all ads.
Use this method only if your application has its own volume controls.
Swift
CAS.settings.setMuteAdSounds(to: false)
Objective-C
[[CAS settings] setMuteAdSoundsTo:NO];
Sometimes a situation occurs when filling Rewarded ads is not enough, in this case, you can allow the display of Interstitial ads to receiving a reward in any case. This option will compare ad cost and serve regular interstitial ads when rewarded video ads are expected to generate less revenue.
Interstitial Ads does not require to watch the video to the end, but the CASCallback.didCompletedAd()
event will be invoked in any case.
Enabled by default.
Swift
CAS.settings.setInterstitialAdsWhenVideoCostAreLower(allow:true)
Objective-C
[[CAS settings] setInterstitialAdsWhenVideoCostAreLowerWithAllow:YES];
- All the rootViewController parameters in Ad APIs must be provided to process ad redirects. In the SDK, all redirects use the present method. Therefore, make sure that the passed
rootViewController
parameters are not null and do not have other present controllers. Otherwise the present will fail becausepresentedViewController
already exists.
🔗 Done! What’s Next?
- Try another ad format:
- Read more about Impression level data.