画中画 (PiP) 广告会显示在浮动窗口中,该窗口始终位于屏幕上的内容(例如文章、信息流或游戏内容)上方。借助此格式,用户可以在广告保持可见状态的同时与您的应用互动。如需展示不会占据整个屏幕的广告,请选择此格式。 详细了解画中画广告。
本指南介绍了如何使用 Google Mobile Ads SDK 在应用中请求和显示画中画广告。
准备工作
在继续操作之前,请先完成以下事项:
安装 Google Mobile Ads SDK 版本
13.10.0或更高版本。启用测试广告,并使用以下测试广告单元 ID:
ca-app-pub-3940256099942544/8810945611
加载广告
如需加载 GADPictureInPictureAd 对象,请创建广告请求并调用 load 方法:
Swift
请将 adUnitID 替换为您的广告单元 ID。
Objective-C
请将 kAdUnitID 替换为您的广告单元 ID。
展示广告
如需在屏幕上显示画中画广告,请配置画中画选项并调用 show 方法。以下示例将广告的默认位置和展示范围设置为屏幕:
Swift
private func showPictureInPictureAd() { // Use the loaded PictureInPictureAd instance. guard let pipAd else { print("No ad to show.") return } let options = PictureInPictureAdOptions() // Uses the Google Mobile Ads SDK's default screen position. options.position = .default // Binds the ad lifecycle to the host screen. options.presentationScope = .screen pipAd.show(with: options) }
Objective-C
- (void)showPictureInPictureAd { // Use the loaded PictureInPictureAd instance. if (!self.pipAd) { NSLog(@"No ad to show."); return; } GADPictureInPictureAdOptions *options = [[GADPictureInPictureAdOptions alloc] init]; // Uses the Google Mobile Ads SDK's default screen position. options.position = GADPictureInPictureAdPositionDefault; // Binds the ad lifecycle to the host screen. options.presentationScope = GADPictureInPictureAdPresentationScopeScreen; [self.pipAd showWithOptions:options]; }
设置位置
默认情况下,Google Mobile Ads SDK 在首次显示时会在屏幕右下角显示画中画广告,如果之前显示过,则会在上次已知的位置显示。如需自定义广告的显示位置,请在画中画选项中设置位置。以下示例将位置设置为屏幕左上角的内容上方:
Swift
private func createTopLeftPositionOptions() -> PictureInPictureAdOptions { let options = PictureInPictureAdOptions() // Sets the ad position to the top-left corner of the screen. options.position = .topLeft return options }
Objective-C
- (GADPictureInPictureAdOptions *)createTopLeftPositionOptions { GADPictureInPictureAdOptions *options = [[GADPictureInPictureAdOptions alloc] init]; // Sets the ad position to the top-left corner of the screen. options.position = GADPictureInPictureAdPositionTopLeft; return options; }
如需查看所有可申请的职位,请访问 GADPictureInPictureAdPosition。
设置演示范围
默认情况下,Google Mobile Ads SDK 将画中画广告绑定到当前宿主屏幕。当宿主屏幕的视图层次结构不再位于内存中时,Google Mobile Ads SDK 会关闭广告。如需在从内存中移除宿主屏幕后仍保持广告可见,请将展示范围设置为应用:
Swift
private func createApplicationScopedOptions() -> PictureInPictureAdOptions { let options = PictureInPictureAdOptions() // Keeps the ad visible beyond the host screen's lifecycle. options.presentationScope = .application return options }
Objective-C
- (GADPictureInPictureAdOptions *)createApplicationScopedOptions { GADPictureInPictureAdOptions *options = [[GADPictureInPictureAdOptions alloc] init]; // Keeps the ad visible beyond the host screen's lifecycle. options.presentationScope = GADPictureInPictureAdPresentationScopeApplication; return options; }
如需了解详情,请参阅让广告在整个屏幕上保持可见。
设置广告事件回调
如需处理画中画广告生命周期事件,请在展示广告之前设置广告的事件回调。此回调会报告标准事件,例如点击和展示。此回调还会报告画中画专用事件,例如广告显示或隐藏时:
Swift
func pictureInPictureAdDidShow(_ pictureInPictureAd: PictureInPictureAd) { print("Picture-in-Picture ad shown.") } func pictureInPictureAdDidHide(_ pictureInPictureAd: PictureInPictureAd) { print("Picture-in-Picture ad hidden.") } func pictureInPictureAdDidFailToShow( _ pictureInPictureAd: PictureInPictureAd, error: Error ) { print("Picture-in-Picture ad failed to show: \(error.localizedDescription)") } func pictureInPictureAdDidRecordImpression( _ pictureInPictureAd: PictureInPictureAd ) { print("Picture-in-Picture ad recorded an impression.") } func pictureInPictureAdDidRecordClick( _ pictureInPictureAd: PictureInPictureAd ) { print("Picture-in-Picture ad recorded a click.") } func pictureInPictureAdWillPresentScreen( _ pictureInPictureAd: PictureInPictureAd ) { print("Picture-in-Picture ad will present screen.") } func pictureInPictureAdWillDismissScreen( _ pictureInPictureAd: PictureInPictureAd ) { print("Picture-in-Picture ad will dismiss screen.") } func pictureInPictureAdDidDismissScreen( _ pictureInPictureAd: PictureInPictureAd ) { print("Picture-in-Picture ad dismissed screen.") }
Objective-C
- (void)pictureInPictureAdDidShow:(GADPictureInPictureAd *)pictureInPictureAd { NSLog(@"Picture-in-Picture ad shown."); } - (void)pictureInPictureAdDidHide:(GADPictureInPictureAd *)pictureInPictureAd { NSLog(@"Picture-in-Picture ad hidden."); } - (void)pictureInPictureAdDidFailToShow:(GADPictureInPictureAd *)pictureInPictureAd withError:(NSError *)error { NSLog(@"Picture-in-Picture ad failed to show: %@", error); } - (void)pictureInPictureAdDidRecordImpression:(GADPictureInPictureAd *)pictureInPictureAd { NSLog(@"Picture-in-Picture ad recorded an impression."); } - (void)pictureInPictureAdDidRecordClick:(GADPictureInPictureAd *)pictureInPictureAd { NSLog(@"Picture-in-Picture ad recorded a click."); } - (void)pictureInPictureAdWillPresentScreen:(GADPictureInPictureAd *)pictureInPictureAd { NSLog(@"Picture-in-Picture ad will present screen."); } - (void)pictureInPictureAdWillDismissScreen:(GADPictureInPictureAd *)pictureInPictureAd { NSLog(@"Picture-in-Picture ad will dismiss screen."); } - (void)pictureInPictureAdDidDismissScreen:(GADPictureInPictureAd *)pictureInPictureAd { NSLog(@"Picture-in-Picture ad dismissed screen."); }
隐藏广告
如需从屏幕中移除浮动广告,请调用 hide 方法。此方法会调用广告隐藏事件回调:
Swift
private func hidePictureInPictureAd() { // Use the loaded PictureInPictureAd instance. guard let pipAd else { print("No ad to hide.") return } pipAd.hide() }
Objective-C
- (void)hidePictureInPictureAd { // Use the loaded PictureInPictureAd instance. if (!self.pipAd) { NSLog(@"No ad to hide."); return; } [self.pipAd hide]; }
清理广告资源
为避免内存泄漏,请在应用完成广告使用后,舍弃对广告对象的引用。例如,当您的应用不再展示广告或再次与广告互动时。对于屏幕范围的广告,当应用从内存中移除宿主屏幕时,请舍弃引用。对于应用范围的广告,在用户在屏幕之间导航时保留广告引用,并在用户关闭广告时舍弃该引用:
Swift
private func cleanUpPictureInPictureAd() { pipAd?.hide() pipAd = nil }
Objective-C
- (void)cleanUpPictureInPictureAd { [self.pipAd hide]; self.pipAd = nil; }
确保广告在各种屏幕上都能显示
如果您将展示范围设置为应用,即使应用从内存中移除托管屏幕,画中画广告仍会保持可见。当用户离开宿主屏幕时,为了让用户能够与画中画广告互动或关闭该广告,您的应用必须保留对画中画广告的访问权限。我们建议将广告保存在应用级单例或共享状态管理器中,而不是单个屏幕的实例变量中。