在 iOS 中初始化 SDK
重要:应在项目运行时的生命周期早期(在需要展示广告之前)初始化 SDK。
ViewController.h
在 ViewController
标头文件 (.h
) 中,导入 Unity Ads 并设置以下委托:
UnityAdsInitializationDelegate
处理初始化成功或失败的回调。UnityAdsLoadDelegate
处理广告单元加载内容成功或失败的回调。UnityAdsShowDelegate
处理广告单元显示加载的内容成功或失败的回调。
#import <UnityAds/UnityAds.h>
@interface ViewController : UIViewController<UnityAdsInitializationDelegate,
UnityAdsLoadDelegate,
UnityAdsShowDelegate>
@property (assign, nonatomic) BOOL testMode;
ViewController.m
要初始化该 SDK,必须为适当平台引用项目的 Game ID(游戏 ID)。在 Monetization(变现)后台的二级导航菜单中选择 CURRENT PROJECT(当前项目)> Settings(设置),即可找到该 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 中实现基本广告的文档以继续您的实现。