横幅广告

横幅广告是占据应用部分布局的矩形广告。当用户与应用互动时,这类广告会停留在屏幕上(锚定在屏幕顶部或底部,或在用户滚动时内嵌在内容中)。横幅广告可在一段时间后自动刷新。如需了解详情,请参阅 横幅广告概览

本指南介绍了如何开始使用 锚定自适应横幅广告,此类广告可根据您指定的广告宽度针对每种设备优化广告尺寸,从而最大限度地提升广告效果。

锚定自适应横幅广告

锚定自适应横幅广告是宽高比固定的广告,而不是常规的固定尺寸广告。宽高比类似于业界标准的 320*50。在您指定可用的全宽后,系统将根据该宽度返回具有最佳高度的广告。最佳高度不会因来自同一设备的请求而发生变化,并且广告刷新时周围的视图也无需移动。

前提条件

始终使用测试广告进行测试

在构建和测试应用时,请确保使用测试广告,而不是实际投放的广告。否则,可能会导致您的帐号被暂停。

对于 iOS 横幅广告,加载测试广告最简便的方法就是使用下面的专用测试广告单元 ID: ca-app-pub-3940256099942544/2435281174

该测试广告单元 ID 已经过专门配置,可为每个请求返回测试广告。您可以在自己应用的编码、测试和调试过程中随意使用该测试广告单元 ID。只需确保在发布应用前用您自己的广告单元 ID 替换该测试广告单元 ID 即可。

如需详细了解移动广告 SDK 的测试广告如何运作,请参阅测试广告

创建一个 GADBannerView

横幅广告在 GADBannerView 对象中展示,因此集成横幅广告的第一步是在视图层次结构中添加 GADBannerView。这通常通过编程方式或通过 Interface Builder 完成。

以程序化方式

您也可以直接将 GADBannerView 实例化。以下示例展示了如何创建 GADBannerView,使其与屏幕安全区域的底部中心对齐:

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController {

  var bannerView: GADBannerView!

  override func viewDidLoad() {
    super.viewDidLoad()
    
    let viewWidth = view.frame.inset(by: view.safeAreaInsets).width

    // Here the current interface orientation is used. Use
    // GADLandscapeAnchoredAdaptiveBannerAdSizeWithWidth or
    // GADPortraitAnchoredAdaptiveBannerAdSizeWithWidth if you prefer to load an ad of a
    // particular orientation,
    let adaptiveSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(viewWidth)
    bannerView = GADBannerView(adSize: adaptiveSize)

    addBannerViewToView(bannerView)
  }

  func addBannerViewToView(_ bannerView: GADBannerView) {
    bannerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(bannerView)
    view.addConstraints(
      [NSLayoutConstraint(item: bannerView,
                          attribute: .bottom,
                          relatedBy: .equal,
                          toItem: view.safeAreaLayoutGuide,
                          attribute: .bottom,
                          multiplier: 1,
                          constant: 0),
       NSLayoutConstraint(item: bannerView,
                          attribute: .centerX,
                          relatedBy: .equal,
                          toItem: view,
                          attribute: .centerX,
                          multiplier: 1,
                          constant: 0)
      ])
   }
   
}

Objective-C

@import GoogleMobileAds;

@interface ViewController ()

@property(nonatomic, strong) GADBannerView *bannerView;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  
  // Here safe area is taken into account, hence the view frame is used after the
  // view has been laid out.
  CGRect frame = UIEdgeInsetsInsetRect(self.view.frame, self.view.safeAreaInsets);
  CGFloat viewWidth = frame.size.width;

  // Here the current interface orientation is used. If the ad is being preloaded
  // for a future orientation change or different orientation, the function for the
  // relevant orientation should be used.
  GADAdSize adaptiveSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(viewWidth);
  // In this case, we instantiate the banner with desired ad size.
  self.bannerView = [[GADBannerView alloc] initWithAdSize:adaptiveSize];

  [self addBannerViewToView:self.bannerView];
}

- (void)addBannerViewToView:(UIView *)bannerView {
  bannerView.translatesAutoresizingMaskIntoConstraints = NO;
  [self.view addSubview:bannerView];
  [self.view addConstraints:@[
    [NSLayoutConstraint constraintWithItem:bannerView
                               attribute:NSLayoutAttributeBottom
                               relatedBy:NSLayoutRelationEqual
                                  toItem:self.view.safeAreaLayoutGuide
                               attribute:NSLayoutAttributeBottom
                              multiplier:1
                                constant:0],
    [NSLayoutConstraint constraintWithItem:bannerView
                               attribute:NSLayoutAttributeCenterX
                               relatedBy:NSLayoutRelationEqual
                                  toItem:self.view
                               attribute:NSLayoutAttributeCenterX
                              multiplier:1
                                constant:0]
                                ]];
}
  
@end

请注意,在本例中,我们没有提供宽度或高度限制,因为提供的广告尺寸将为横幅广告提供固有内容尺寸,以调整视图大小。

Interface Builder

您可以将 GADBannerView 添加到 storyboard 或 xib 文件中。使用此方法时,请确保仅在横幅广告上添加位置限制。例如,在屏幕底部展示自适应横幅广告时,应将横幅广告视图的底部设置为“底部布局指南”的顶部,并将中心 X 设置为等于父视图的中心 X。

横幅广告的广告尺寸仍以编程方式设置:

Swift

  bannerView.adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(viewWidth)

Objective-C

  self.bannerView.adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(viewWidth);

加载广告

GADBannerView 创建完毕并配置其属性后,就可以加载广告了。通过对 GADRequest 对象调用 loadRequest: 可以做到这一点:

Swift

override func viewDidLoad() {
  super.viewDidLoad()
  ...
  
  //  Set the ad unit ID and view controller that contains the GADBannerView.
  bannerView.adUnitID = "ca-app-pub-3940256099942544/2435281174"
  bannerView.rootViewController = self

  bannerView.load(GADRequest())
}

Objective-C

-   (void)viewDidLoad {
  [super viewDidLoad];
  ...
  
  //  Set the ad unit ID and view controller that contains the GADBannerView.
  self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2435281174";
  self.bannerView.rootViewController = self;

  [self.bannerView loadRequest:[GADRequest request]];
}

GADRequest 对象代表单个广告请求,并包含定位信息等内容的属性。

如果您的广告加载失败,只要您已将广告单元配置为刷新,就无需明确请求另一个广告;Google 移动广告 SDK 会按照您在 AdMob界面中指定的刷新频率。如果您尚未启用刷新,则需要发出新的请求。

广告事件

通过使用 GADBannerViewDelegate,您可以监听各种生命周期事件,例如广告何时关闭、用户何时离开应用等。

注册横幅广告事件

若要注册横幅广告事件,请将 GADBannerView 上的 delegate 属性设置为实现 GADBannerViewDelegate 协议的对象。一般情况下,实现横幅广告的类也充当代理类,在这种情况下,可将 delegate 属性设置为 self

Swift

import GoogleMobileAds
import UIKit

class ViewController: UIViewController, GADBannerViewDelegate {

  var bannerView: GADBannerView!

  override func viewDidLoad() {
    super.viewDidLoad()
    ...
    bannerView.delegate = self
  }
}

Objective-C

@import GoogleMobileAds;

@interface ViewController () <GADBannerViewDelegate>

@property(nonatomic, strong) GADBannerView *bannerView;

@end

@implementation ViewController

-   (void)viewDidLoad {
  [super viewDidLoad];
  ...
  self.bannerView.delegate = self;
}

实现横幅广告事件

GADBannerViewDelegate 中的每个方法都标记为可选,因此您只需实现所需的方法。以下示例实现了每个方法,并将消息记录到控制台:

Swift

func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
  print("bannerViewDidReceiveAd")
}

func bannerView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: Error) {
  print("bannerView:didFailToReceiveAdWithError: \(error.localizedDescription)")
}

func bannerViewDidRecordImpression(_ bannerView: GADBannerView) {
  print("bannerViewDidRecordImpression")
}

func bannerViewWillPresentScreen(_ bannerView: GADBannerView) {
  print("bannerViewWillPresentScreen")
}

func bannerViewWillDismissScreen(_ bannerView: GADBannerView) {
  print("bannerViewWillDIsmissScreen")
}

func bannerViewDidDismissScreen(_ bannerView: GADBannerView) {
  print("bannerViewDidDismissScreen")
}

Objective-C

-   (void)bannerViewDidReceiveAd:(GADBannerView *)bannerView {
  NSLog(@"bannerViewDidReceiveAd");
}

-   (void)bannerView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(NSError *)error {
  NSLog(@"bannerView:didFailToReceiveAdWithError: %@", [error localizedDescription]);
}

-   (void)bannerViewDidRecordImpression:(GADBannerView *)bannerView {
  NSLog(@"bannerViewDidRecordImpression");
}

-   (void)bannerViewWillPresentScreen:(GADBannerView *)bannerView {
  NSLog(@"bannerViewWillPresentScreen");
}

-   (void)bannerViewWillDismissScreen:(GADBannerView *)bannerView {
  NSLog(@"bannerViewWillDismissScreen");
}

-   (void)bannerViewDidDismissScreen:(GADBannerView *)bannerView {
  NSLog(@"bannerViewDidDismissScreen");
}

如需了解 iOS API Demo 应用中横幅广告委托方法的实现方式,请参阅广告委托示例。

Swift Objective-C

使用场景

以下是这些广告事件方法的一些使用情形示例。

在收到广告后将横幅广告添加到视图层次结构中

您可能需要延迟将 GADBannerView 添加到视图层次结构中,直到收到广告为止。为此,您可以监听 bannerViewDidReceiveAd: 事件:

Swift

func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
  // Add banner to view and add constraints as above.
  addBannerViewToView(bannerView)
}

Objective-C

-   (void)bannerViewDidReceiveAd:(GADBannerView *)bannerView {
  // Add bannerView to view and add constraints as above.
  [self addBannerViewToView:self.bannerView];
}

以动画方式展示横幅广告

在横幅广告返回后,您也可以利用 bannerViewDidReceiveAd: 事件以动画方式展示该广告,如以下示例所示:

Swift

func bannerViewDidReceiveAd(_ bannerView: GADBannerView) {
  bannerView.alpha = 0
  UIView.animate(withDuration: 1, animations: {
    bannerView.alpha = 1
  })
}

Objective-C

-   (void)bannerViewDidReceiveAd:(GADBannerView *)bannerView {
  bannerView.alpha = 0;
  [UIView animateWithDuration:1.0 animations:^{
    bannerView.alpha = 1;
  }];
}

暂停和恢复应用

GADBannerViewDelegate 协议包含用于通知某些事件的方法,例如点击导致呈现或关闭叠加层等。如果您希望跟踪这些事件是否由于广告而发生的,请注册这些 GADBannerViewDelegate 方法。

若要捕获所有类型(而不仅仅是由于广告点击而发生)的叠加层展示或外部浏览器调用,您的应用最好监听 UIViewControllerUIApplication 上的相同方法。下表显示了可与 GADBannerViewDelegate 方法同时调用的等效 iOS 方法:

GADBannerViewDelegate 方法 iOS 方法
bannerViewWillPresentScreen: UIViewController 的 viewWillDisappear:
bannerViewWillDismissScreen: UIViewController 的 viewWillAppear:
bannerViewDidDismissScreen: UIViewController 的 viewDidAppear:

其他资源

GitHub 上的示例

后续步骤

折叠式横幅广告

折叠式横幅广告是一种横幅广告,最初以较大的叠加层形式展示,并带有一个可将广告收起为较小尺寸的按钮。请考虑使用它进一步优化您的性能。如需了解详情,请参阅折叠式横幅广告

内嵌自适应横幅广告

与锚定自适应横幅广告相比,内嵌自适应横幅广告是一种更大、更高的横幅广告。它们的高度可变,可以和设备屏幕一样高。对于在可滚动内容中投放横幅广告的应用,建议使用内嵌自适应横幅广告,而不是锚定自适应横幅广告。如需了解详情,请参阅内嵌自适应横幅广告

浏览其他主题