本指南将介绍如何使用 iOS SDK 实现您自己的自定义广告界面。 为此,您需要停用默认界面,设置新的自定义界面,然后 使用从 SDK 获取的广告信息填充新界面。本指南以 Objective-C 基本示例 。您可以下载完整的 自定义界面示例。
定义新的界面元素
在编写任何代码之前,先修改故事板,添加 了解详情按钮、跳过广告按钮和倒计时器。请确保 跳过广告按钮是一个“自定义”按钮按钮(如下所示), 停止闪烁。
了解详情按钮

跳过广告按钮

广告倒计时标签

确保这些新元素已关联到 ViewController 中的变量。
此外,还要添加变量来跟踪当前广告,以及一直到用户跳过广告前的等待时间。
稍后会用到
ViewController.m
@property(nonatomic, weak) IBOutlet UIButton *learnMore;
@property(nonatomic, weak) IBOutlet UIButton *skipAd;
@property(nonatomic, weak) IBOutlet UILabel *adCountdown;
@property(nonatomic) NSTimeInterval timeTillSkip;
@property(nonatomic, strong) IMAAd *currentAd;
停用内置界面
首先,告知 SDK 您想要停用其内置界面。
ViewController.m
- (void)setUpContentPlayer { ... IMAAdsRenderingSettings *adsRenderingSettings = [[IMAAdsRenderingSettings alloc] init]; adsRenderingSettings.disableUi = YES; [self.adsManager initializeWithAdsRenderingSettings:adsRenderingSettings]; }
隐藏自定义界面,仅在允许的情况下显示
某些 Google 广告(例如 AdSense 广告)不允许自定义界面。 它们始终会改为呈现自己的界面。默认隐藏自定义界面:
ViewController.m
- (void)viewDidLoad { ... [self hideCustomUi]; self.timeTillSkip = INFINITY; self.learnMore.layer.zPosition = MAXFLOAT; self.skipAd.layer.zPosition = MAXFLOAT; self.adCountdown.layer.zPosition = MAXFLOAT; ... } - (void)hideCustomUi { self.learnMore.hidden = YES; self.adCountdown.hidden = YES; self.skipAd.hidden = YES; }
仅当当前正在播放的广告隐藏了其界面时才显示自定义界面。 隐藏各个广告后的自定义界面,以防后续广告跟不上 允许自定义界面:
ViewController.m
- (void)adsManager:(IMAAdsManager *)adsManager didReceiveAdEvent:(IMAAdEvent *)event { // When the SDK notified you that ads have been loaded, play them. if (event.type == kIMAAdEvent_LOADED) { [adsManager start]; } else if (event.type == kIMAAdEvent_STARTED) { self.currentAd = event.ad; if (self.currentAd.isUiDisabled) { [self showCustomUi]; } } else if (event.type == kIMAAdEvent_SKIPPED || event.type == kIMAAdEvent_COMPLETE) { [self hideCustomUi]; } else if (event.type == kIMAAdEvent_TAPPED) { // Since you're disabling IMA's built-in UI, you're also losing the // UI element that resumes paused ads with a tap. Add this code // to resume paused ads when a user taps on them. [self.adsManager resume]; } } - (void)showCustomUi { self.learnMore.hidden = NO; [self.videoView bringSubviewToFront:self.learnMore]; self.adCountdown.hidden = NO; if (self.currentAd.isSkippable) { self.skipAd.hidden = NO; [self.videoView bringSubviewToFront:self.skipAd]; } else { self.skipAd.hidden = YES; } self.adCountdown.text = @""; [self.skipAd setTitle:@"" forState:UIControlStateNormal]; }
为“了解详情”按钮添加逻辑
连接的第一个界面组件是了解详情按钮。 制作“Touch Up Inside”来通知 SDK 已点击了解详情按钮。
ViewController.m
- (IBAction)onLearnMoreTouch:(id)sender {
  [self.adsManager clicked];
}
为倒计时器添加逻辑
接着,将倒数计时器连线
adsManager:adDidProgressToTime:totalTime:即可计算
广告的剩余时间。
ViewController.m
- (void)adsManager:(IMAAdsManager *)adsManager
adDidProgressToTime:(NSTimeInterval)mediaTime
         totalTime:(NSTimeInterval)totalTime {
  // Update countdown timer.
  NSMutableString *countdownText = [NSMutableString stringWithString:@"Ad "];
  NSInteger totalAds = self.currentAd.adPodInfo.totalAds;
  if (totalAds > 1) {
    NSInteger position = self.currentAd.adPodInfo.adPosition;
    [countdownText appendString:
        [NSString stringWithFormat:@"%ld of %ld", (long)position, (long)totalAds]];
  }
  NSTimeInterval remainingTime = totalTime - mediaTime;
  [countdownText appendString:[NSString stringWithFormat:@" (%.fs)", remainingTime]];
  self.adCountdown.text = countdownText;
}
为“跳过广告”按钮添加逻辑
最后,连接跳过广告按钮。此按钮仅针对可跳过式广告显示 广告;倒计时到 0 时便会跳过广告。已添加此代码 与上述倒计时器所用的方法相同。
ViewController.m
- (void)adsManager:(IMAAdsManager *)adsManager
adDidProgressToTime:(NSTimeInterval)mediaTime
         totalTime:(NSTimeInterval)totalTime {
  ...
  // Update skip button
  if (self.currentAd.isSkippable) {
    self.timeTillSkip = self.currentAd.skipTimeOffset - mediaTime;
    NSString *skipString = @"Skip ad";
    if (self.timeTillSkip > 0) {
      skipString =
          [NSString stringWithFormat:@"Skip this ad in %.f...", self.timeTillSkip];
    }
    // Disable animations while you change the button text to prevent it from blinking. The button
    // type must be "Custom" instead of "System" for this to work. This can be set in the attributes
    // inspector for the button in the storyboard file.
    [UIView setAnimationsEnabled:NO];
    [self.skipAd setTitle:skipString forState:UIControlStateNormal];
    [self.skipAd layoutIfNeeded];
    [UIView setAnimationsEnabled:YES];
  }
}
实现之后,我们的 自定义界面示例 如下所示:

问题排查
- 您是否有用于停用广告界面的示例代码?
- 您可以复制此 示例代码 并将其粘贴到您的 IMA 实现中。
- 我无法停用默认界面。
- 请检查并确保您已将 adsRenderingSettings.disableUi设置为true并将其传递给getAdsManager。检查并确认ad.isUiDisabled()返回true。此外,您必须在 Ad Manager 中启用相应广告联盟,才能停用 。如果您已启用,您的 VAST 将包含一个Extension, 如下所示:<Extension type="uiSettings"> <UiHideable>1</UiHideable> </Extension> <UiHideable>值为 0 的脚本。如果遇到这种情况, 广告投放管理团队需要进行更改以确保这些类型的广告不会投放。