Initialize CAS - cleveradssolutions/CAS-iOS GitHub Wiki
⚡ Before you start
Make sure you have correctly Project setup.
- Import framework to scripts
- Create MediationManager
- Ads processing
- Retrieve the Version Number
- Targeting options
Swift:
import CleverAdsSolutions
Objective-C:
@import CleverAdsSolutions;
In most cases, a casID
is the same as your application store ID. In a real app, it is important that you use your actual CAS manager ID.
Important
Do not initialize mediated advertising SDKs (CAS does that for you).
Not following this step will result in noticeable integration issues.
Swift
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
/* Class body ... */
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Configure CAS.settings before initialize
// Configure CAS.targetingOptions before initialize
let manager = getAdManager()
return true
}
func getAdManager() -> CASMediationManager {
return CAS.buildManager()
// List Ad formats used in app
.withAdTypes(CASType.banner, CASType.interstitial, CASType.rewarded)
// Set Test ads or live ads
#if DEBUG
.withTestAdMode(true)
#endif
.withCompletionHandler({ initialConfig in
if let error = initialConfig.error {
print("CAS Initialization failed: \(error)")
} else {
print("CAS Initialization completed")
}
let userCountryISO2: String? = initialConfig.countryCode
let protectionApplied: Bool = initialConfig.isConsentRequired
let manager: CASMediationManager = initialConfig.manager
})
// Set your CAS ID
.create(withCasId: casID)
}
}
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
...
}
Objective-C
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
@implementation AppDelegate
/* Class body ... */
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Configure [CAS settings] before initialize
// Configure [CAS targetingOptions] before initialize
CASMediationManager* manager = [self getAdManager];
return YES;
}
- (CASMediationManager *)getAdManager{
CASManagerBuilder* builder = [CAS buildManager];
// List Ad formats used in app
[builder withAdFlags:CASTypeFlagsBanner | CASTypeFlagsInterstitial | CASTypeFlagsRewarded];
[builder withTestAdMode:isTestBuild];
[builder withCompletionHandler:^(CASInitialConfig *config) {
if (config.error) {
NSLog(@"CAS Initialization failed: %@", config.error);
} else {
NSLog(@"CAS Initialization completed");
}
}];
return [builder createWithCasId:casID];
}
@end
Important
Some third party networks require a window: UIWindow?
property in the AppDelegate
, even when SceneDelegate
is used. Missing the property will cause Fatal Error:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[MyApp.AppDelegate window]: unrecognized selector sent to instance 0x282efc980'
CAS have functionality to limit the processing of specific CASType
.
This can be useful when you need to improve application performance by turning off unused formats.
Or if you have determined that the user will no longer need to be shown ads, for example after purchase.
List the CASType
that will be used in your application:
.withAdTypes(CASType.banner, CASType.interstitial, CASType.rewarded)
🌟 Your choice helps us configuring mediation in the most effective way for you, depending on the chosen ad formats.
Also, the state can be changed after initialization using the following methods:
manager.setEnabled(true, type: CASType.Interstitial)
- If processing is inactive then all calls to the selected ad type will fail with error
CASError.managerIsDisabled
. - The state will not be saved between sessions.
⚠️ Turning off the processing leads to the complete destruction of the ready ads from memory. And reactivation after destruction requires a new load of ads to memory.
To programmatically retrieve the SDK version number at runtime, CAS provides the following method:
String sdkVersion = CAS.getSDKVersion();
🔗 Done! What’s Next?
- Explore and configure Targeting options
- Follow the instructions below to Test the integration:
- Learn about Privacy Regulations