在 iOS 上初始化 SDK

重要:在您需要展示广告之前,在项目的运行时生命周期中尽早初始化 SDK。

ViewController.h

在您的 ViewController 头文件 (.h) 中,导入 Unity Ads 并设置以下委托

#import <UnityAds/UnityAds.h>
 
@interface ViewController : UIViewController<UnityAdsInitializationDelegate,
UnityAdsLoadDelegate,
UnityAdsShowDelegate>
 
@property (assign, nonatomic) BOOL testMode;

ViewController.m

要初始化 SDK,您必须引用项目的相应平台的 Game ID。您可以在 Unity Ads 货币化控制面板 中找到 ID,方法是选择辅助导航菜单中的 当前项目 > 设置

在您的 ViewController 实现 (.m) 中,创建一个 UnityAdsInitializationDelegate 委托来处理初始化回调,并将它作为参数引用在 initialize 方法中。

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

下一步:要继续实施,请参考 在 iOS 中实施基本广告 文档。