Initializing the SDK in iOS

ViewController.h

In your ViewController header (.h), import Unity Ads and set the following delegates:

Copy
#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 Monetize 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.

Copy
- (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);
}

Important: Initialize the SDK early in your game’s run-time life cycle, before you need to show ads.

Note: See the documentation on implementing basic ads in iOS to continue your implementation.