Initializing the SDK in iOS
Important: Initialize the SDK early in your project’s run-time life cycle before you need to show ads.
ViewController.h
In your ViewController
header (.h
), import Unity Ads and set the following delegates:
UnityAdsInitializationDelegate
handles callbacks for initialization succeeding or failing.UnityAdsLoadDelegate
handles callbacks for Ad Units succeeding or failing to load content.UnityAdsShowDelegate
handles callbacks for Ad Units succeeding or failing to show loaded content.
#import <UnityAds/UnityAds.h>
@interface ViewController : UIViewController<UnityAdsInitializationDelegate,
UnityAdsLoadDelegate,
UnityAdsShowDelegate>
@property (assign, nonatomic) BOOL testMode;
ViewController.m
To initialize the SDK, you must reference your project’s Game ID for the appropriate platform. You can locate the ID in the Unity Ads Monetization dashboard by selecting CURRENT PROJECT > Settings from the secondary navigation menu
In your ViewController
implementation (.m
), create a UnityAdsInitializationDelegate
delegate to handle initialization callbacks, and reference it as a parameter in the initialize
method.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[UnityAds
initialize:kDefaultGameId
testMode:self.testMode
initializationDelegate:self];
}
// Implement initialization callbacks to handle success or failure:
#pragma mark : UnityAdsInitializationDelegate
- (void)initializationComplete {
NSLog(@" - UnityAdsInitializationDelegate initializationComplete" );
// Pre-load an ad when initialization succeeds, so it is ready to show:
[UnityAds load:@"Interstitial_iOS" loadDelegate:self];
}
- (void)initializationFailed:(UnityAdsInitializationError)error withMessage:(NSString *)message {
NSLog(@" - UnityAdsInitializationDelegate initializationFailed with message: %@", message );
}
// Implement load callbacks to handle success or failure after initialization:
#pragma mark: UnityAdsLoadDelegate
- (void)unityAdsAdLoaded:(NSString *)placementId {
NSLog(@" - UnityAdsLoadDelegate unityAdsAdLoaded %@", placementId);
}
- (void)unityAdsAdFailedToLoad:(NSString *)placementId
withError:(UnityAdsLoadError)error
withMessage:(NSString *)message {
NSLog(@" - UnityAdsLoadDelegate unityAdsAdFailedToLoad %@", placementId);
}
Next steps: To continue your implementation, refer to the Implementing basic ads in iOS documentation.