植入挤压式广告(Beta 版)

缩屏广告

Squeezeback会将应用内容缩减并嵌入到较小的视图中,同时在腾出的周围空间内展示原生广告。这种广告格式让用户在与您的应用互动时,广告依然清晰可见。如需展示不覆盖应用内容的广告,请选择此格式。如需了解详情,请参阅使用原生高级广告格式的 Squeezeback 广告。

本指南介绍了如何使用 Google Mobile Ads SDK 在应用中请求和展示 squeezeback 广告。

准备工作

在继续之前,请确保满足以下条件:

在不同界面间管理广告生命周期

默认情况下,当用户在不同屏幕间跳转时,squeezeback 广告会保持可见。为了处理广告事件或在其他屏幕上隐藏广告,您的应用必须保留对 squeezeback 广告的访问权限。我们建议将广告对象保留在应用级单例或共享状态管理器中,而非仅将其作为单个屏幕的实例变量。

加载广告

如需加载 GADSqueezebackAd 对象,请创建一个广告请求并调用 load 方法。以下示例会保存加载的 squeezeback 广告、注册广告事件,并在失败时记录错误:

Swift

private func loadSqueezebackAd() async {
  do {
    // Capture the SqueezebackAd reference for later use.
    squeezebackAd = try await SqueezebackAd.load(
      with: adUnitID, request: Request())
    // Set the delegate to be notified of ad events.
    squeezebackAd?.delegate = self
  } catch {
    print("Squeezeback ad failed to load: \(error.localizedDescription)")
  }
}

将 adUnitID 替换为您的广告单元 ID。

Objective-C

- (void)loadSqueezebackAd {
  GADRequest *request = [GADRequest request];

  [GADSqueezebackAd loadWithAdUnitID:kAdUnitID
                             request:request
                   completionHandler:^(GADSqueezebackAd *_Nullable ad, NSError *_Nullable error) {
                     if (error) {
                       NSLog(@"Squeezeback ad failed to load: %@", error);
                       return;
                     }
                     // Capture the GADSqueezebackAd reference for later use.
                     self.squeezebackAd = ad;
                     // Set the delegate to be notified of ad events.
                     self.squeezebackAd.delegate = self;
                   }];
}

将 kAdUnitID 替换为您的广告单元 ID。

展示广告

如需在屏幕上展示 squeezeback 广告,请调用 show 方法:

Swift

private func showSqueezebackAd() {
  // Use the loaded SqueezebackAd instance.
  guard let squeezebackAd else {
    print("No ad to show.")
    return
  }
  let options = SqueezebackAdOptions()
  squeezebackAd.show(with: options)
}

Objective-C

- (void)showSqueezebackAd {
  // Use the loaded GADSqueezebackAd instance.
  if (!self.squeezebackAd) {
    NSLog(@"No ad to show.");
    return;
  }
  GADSqueezebackAdOptions *options = [[GADSqueezebackAdOptions alloc] init];
  [self.squeezebackAd showWithOptions:options];
}

Squeezeback 广告仅在竖屏模式下展示。如果您在横屏模式下调用 show 方法,广告将无法展示。将设备转为横屏模式或展示全屏广告会隐藏正在展示的广告。Google Mobile Ads SDK 每次在每个窗口场景中最多展示一个 squeezeback 广告。

设置后备背景颜色

默认情况下,Google Mobile Ads SDK 会在缩小的应用内容和广告后面显示深灰色背景。如需自定义此颜色,请在展示广告时在广告选项中指定后备背景颜色。以下示例将后备背景颜色设置为深灰色:

Swift

private func createFallbackBackgroundColorOptions() -> SqueezebackAdOptions {
  let options = SqueezebackAdOptions()
  // Sets the fallback background color behind the ad and squeezed content.
  options.fallbackBackgroundColor = .darkGray
  return options
}

Objective-C

- (GADSqueezebackAdOptions *)createFallbackBackgroundColorOptions {
  GADSqueezebackAdOptions *options = [[GADSqueezebackAdOptions alloc] init];
  // Sets the fallback background color behind the ad and squeezed content.
  options.fallbackBackgroundColor = UIColor.darkGrayColor;
  return options;
}

设置窗口场景

默认情况下,Google Mobile Ads SDK 会自动解析活动窗口场景。如需指定 squeezeback 广告在哪个 UIWindowScene 对象中展示,请在展示广告时设置 squeezeback 广告选项的 windowScene 属性。以下示例在 squeezeback 广告选项中设置了窗口场景:

Swift

private func createOptions(with windowScene: UIWindowScene)
  -> SqueezebackAdOptions
{
  let options = SqueezebackAdOptions()
  // Sets the window scene where the squeezeback ad will be presented.
  options.windowScene = windowScene
  return options
}

Objective-C

- (GADSqueezebackAdOptions *)createOptionsWithWindowScene:(UIWindowScene *)windowScene {
  GADSqueezebackAdOptions *options = [[GADSqueezebackAdOptions alloc] init];
  // Sets the window scene where the squeezeback ad will be presented.
  options.windowScene = windowScene;
  return options;
}

监听广告事件

如需处理 squeezeback 广告生命周期事件,请在展示广告之前注册广告事件。这些事件会报告广告互动,例如点击和展示。这些事件还会报告 squeezeback 特有的事件,例如广告显示、无法显示或隐藏时。

Swift

func squeezebackAdDidShow(_ squeezebackAd: SqueezebackAd) {
  print("Squeezeback ad shown.")
}

func squeezebackAdDidHide(_ squeezebackAd: SqueezebackAd) {
  // Called when hide is invoked or the user taps the close button.
  // After the user taps the close button, the ad can't be shown again.
  print("Squeezeback ad hidden.")
}

func squeezebackAdDidFailToShow(
  _ squeezebackAd: SqueezebackAd, error: Error
) {
  print("Squeezeback ad failed to show: \(error.localizedDescription)")
}

func squeezebackAdDidRecordImpression(_ squeezebackAd: SqueezebackAd) {
  print("Squeezeback ad recorded an impression.")
}

func squeezebackAdDidRecordClick(_ squeezebackAd: SqueezebackAd) {
  print("Squeezeback ad recorded a click.")
}

func squeezebackAd(
  _ squeezebackAd: SqueezebackAd, didRecordPaidEventWith value: AdValue
) {
  print(
    "Squeezeback ad recorded a paid event: \(value.value) \(value.currencyCode)."
  )
}

func squeezebackAdWillPresentScreen(_ squeezebackAd: SqueezebackAd) {
  print("Squeezeback ad will present screen.")
}

func squeezebackAdWillDismissScreen(_ squeezebackAd: SqueezebackAd) {
  print("Squeezeback ad will dismiss screen.")
}

func squeezebackAdDidDismissScreen(_ squeezebackAd: SqueezebackAd) {
  print("Squeezeback ad dismissed screen.")
}

Objective-C

- (void)squeezebackAdDidShow:(GADSqueezebackAd *)squeezebackAd {
  NSLog(@"Squeezeback ad shown.");
}

- (void)squeezebackAdDidHide:(GADSqueezebackAd *)squeezebackAd {
  // Called when hide is invoked or the user taps the close button.
  // After the user taps the close button, the ad can't be shown again.
  NSLog(@"Squeezeback ad hidden.");
}

- (void)squeezebackAdDidFailToShow:(GADSqueezebackAd *)squeezebackAd withError:(NSError *)error {
  NSLog(@"Squeezeback ad failed to show: %@", error);
}

- (void)squeezebackAdDidRecordImpression:(GADSqueezebackAd *)squeezebackAd {
  NSLog(@"Squeezeback ad recorded an impression.");
}

- (void)squeezebackAdDidRecordClick:(GADSqueezebackAd *)squeezebackAd {
  NSLog(@"Squeezeback ad recorded a click.");
}

- (void)squeezebackAd:(GADSqueezebackAd *)squeezebackAd
    didRecordPaidEventWithAdValue:(GADAdValue *)value {
  NSLog(@"Squeezeback ad recorded a paid event: %@ %@.", value.value, value.currencyCode);
}

- (void)squeezebackAdWillPresentScreen:(GADSqueezebackAd *)squeezebackAd {
  NSLog(@"Squeezeback ad will present screen.");
}

- (void)squeezebackAdWillDismissScreen:(GADSqueezebackAd *)squeezebackAd {
  NSLog(@"Squeezeback ad will dismiss screen.");
}

- (void)squeezebackAdDidDismissScreen:(GADSqueezebackAd *)squeezebackAd {
  NSLog(@"Squeezeback ad dismissed screen.");
}

用户关闭广告后,Google Mobile Ads SDK 会调用广告隐藏事件,您无法再次展示该广告。

隐藏广告

如需移除广告并将应用内容恢复为完整尺寸,请调用 hide 方法。此方法会调用广告隐藏事件。以下示例会检查是否存在已保存的 squeezeback 广告引用,如果存在,则隐藏该广告:

Swift

private func hideSqueezebackAd() {
  // Use the loaded SqueezebackAd instance.
  guard let squeezebackAd else {
    print("No ad to hide.")
    return
  }
  squeezebackAd.hide()
}

Objective-C

- (void)hideSqueezebackAd {
  // Use the loaded GADSqueezebackAd instance.
  if (!self.squeezebackAd) {
    NSLog(@"No ad to hide.");
    return;
  }
  [self.squeezebackAd hide];
}

清理广告资源

为避免内存泄漏,请在用户在不同屏幕间跳转时保留广告引用。当用户关闭广告或应用使用完广告后,请释放该引用。以下示例会清理广告:

Swift

private func cleanUpSqueezebackAd() {
  squeezebackAd?.hide()
  squeezebackAd = nil
}

Objective-C

- (void)cleanUpSqueezebackAd {
  [self.squeezebackAd hide];
  self.squeezebackAd = nil;
}