添加了随播广告支持
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本指南适用于希望向其 iOS IMA 实现添加随播广告的发布商。
前提条件
- 已集成 IMA SDK 的 iOS 应用。
- 配置为返回随播广告的广告代码。
实用的入门信息
如果您仍需要在应用中实现 IMA SDK,请查看我们的入门指南。
向应用添加随播广告
为随播广告创建 UIView
在请求伴奏之前,您需要在布局中为其创建一个空间。在故事板中,将一个视图拖放到 ViewController
上,并调整其大小以适合随播广告。然后,确保您的随播广告位与实现中的变量相关联(此示例使用名为 companionView 的变量)。在下面的屏幕截图中,浅灰色视图是随播广告视图:
创建 IMACompanionAdSlot
下一步是从视图构建 IMACompanionAdSlot 对象。IMA SDK 会使用 VAST 响应中尺寸与视图的高度和宽度相匹配的任何随播广告填充随播广告位。IMA SDK 还支持使用自适应尺寸的随播广告。
ViewController.h
@property(nonatomic, weak) IBOutlet UIView *companionView;
ViewController.m
self.companionSlot =
[[IMACompanionAdSlot alloc] initWithView:self.companionView
width:self.companionView.frame.size.width
height:self.companionView.frame.size.height];
将随播广告位传递给广告容器
最后,您需要将随播广告位的数组传递给 IMAAdDisplayContainer
构造函数,以告知 SDK 存在此随播广告位:
ViewController.m
return [[IMAAdDisplayContainer alloc] initWithAdContainer:self.videoView
viewController:self
companionSlots:@[ self.companionSlot ]];
这就是全部内容!您的应用现在正在展示随播广告。
展示自适应随播广告
IMA 现在支持自适应随播广告。这些随播广告可以调整大小,以匹配广告位的尺寸。它们会占据父视图 100% 的宽度,然后调整高度以适应随播广告的内容。它们通过 Ad Manager 中的Fluid
随播广告尺寸进行设置。
请参阅下图,了解在何处设置此值。
更新 iOS 应用以支持流畅的配套设备
您可以通过仅使用 view
参数(排除 width
和 height
)来启动 IMACompanionAdSlot
,从而声明一个自适应随播广告位。
ViewController.m
self.companionSlot =
[[IMACompanionAdSlot alloc] initWithView:self.companionView];
常见问题解答
- 我按照指南操作了,但没有看到随播广告。该怎么做?
- 首先,检查以确保代码确实返回了随播广告。为此,请在网络浏览器中打开代码,然后查找 CompanionAds 代码。如果您看到这种情况,请检查返回的随播广告的尺寸是否与您尝试显示该广告的 UIView 的尺寸相同。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThis guide helps iOS publishers integrate companion ads using the IMA SDK.\u003c/p\u003e\n"],["\u003cp\u003eYou need an iOS app with the IMA SDK and an ad tag returning companion ads to get started.\u003c/p\u003e\n"],["\u003cp\u003eCreate a UIView for the companion ad and an IMACompanionAdSlot tied to it.\u003c/p\u003e\n"],["\u003cp\u003eInform the SDK about the companion slot by passing it to the IMAAdDisplayContainer.\u003c/p\u003e\n"],["\u003cp\u003eFluid companion ads can be implemented for automatic resizing to fit content.\u003c/p\u003e\n"]]],[],null,["Select platform: [HTML5](/interactive-media-ads/docs/sdks/html5/client-side/companions \"View this page for the HTML5 platform docs.\") [Android](/interactive-media-ads/docs/sdks/android/client-side/companions \"View this page for the Android platform docs.\") [iOS](/interactive-media-ads/docs/sdks/ios/client-side/companions \"View this page for the iOS platform docs.\")\n\nThis guide is intended for publishers interested in adding companion ads to their iOS IMA implementation.\n\nPrerequisites\n\n- iOS Application with the IMA SDK integrated.\n- An ad tag configured to return a companion ad.\n - If you need a sample, check out our [FAQ](/interactive-media-ads/docs/sdks/ios/client-side/faq#4).\n\nHelpful primers\n\nIf you still need to implement the IMA SDK in your app, check out our [Get Started guide](/interactive-media-ads/docs/sdks/ios/client-side).\n\nAdd companion ads to your app\n\nCreate a UIView for your companion\n\nBefore requesting a companion, you need to create a space for it in your layout. In your storyboard, drag and drop a View onto your `ViewController` and size it to your companion ad. Then, make sure your companion slot is tied to a variable in your implementation (this example uses a variable called companionView). In the screenshot below, the light gray view is the companion ad view:\n\nCreate an IMACompanionAdSlot\n\nThe next step is to build an IMACompanionAdSlot object from your view. The IMA SDK\npopulates the companion ad slot with any companions from the VAST response that have dimensions\nmatching the view's height and width. The IMA SDK also supports using\n[fluid sized companions](/interactive-media-ads/docs/sdks/ios/client-side/companions#display-fluid-companion-ads).\n**ViewController.h** \n\n```transact-sql\n@property(nonatomic, weak) IBOutlet UIView *companionView;\n```\n**ViewController.m** \n\n```objective-c\nself.companionSlot =\n [[IMACompanionAdSlot alloc] initWithView:self.companionView\n width:self.companionView.frame.size.width\n height:self.companionView.frame.size.height];\n```\n\nPass the companion ad slot to your ad container\n\nLastly, you need to let the SDK know that this companion slot exists by passing an array of your companion slots to the `IMAAdDisplayContainer` constructor:\n**ViewController.m** \n\n```objective-c\nreturn [[IMAAdDisplayContainer alloc] initWithAdContainer:self.videoView\n viewController:self\n companionSlots:@[ self.companionSlot ]];\n```\n\nThat's all there is to it! Your application is now displaying companion ads.\n\nDisplay fluid companion ads\n\nIMA now supports fluid companion ads. These companions ads can resize to match the size of\nthe ad slot. They fill 100% of the width of parent view, then resize their height to fit the\ncompanion's content. They're set by using the `Fluid` companion size in Ad Manager.\nSee the following image for where to set this value.\n\nUpdate iOS apps for fluid companions\n\nYou can declare a fluid companion slot by initiating the\n[`IMACompanionAdSlot`](/interactive-media-ads/docs/sdks/ios/client-side/reference/Classes/IMACompanionAdSlot#-initwithview:)\nwith only the `view` parameter; excluding `width` and\n`height`.\n**ViewController.m** \n\n```objective-c\nself.companionSlot =\n [[IMACompanionAdSlot alloc] initWithView:self.companionView];\n```\n\nFAQ\n\nI followed the guide, but I'm not seeing companion ads. What should I do?\n: First, check to make sure your tag really is returning companions. To do this, open the tag in a web browser and look for a CompanionAds tag. If you see that, check to make sure the size of the companion being returned is the same size as the UIView in which you're trying to display it."]]