新增隨播廣告支援
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本指南適用於有意在 iOS IMA 導入作業中加入隨播廣告的發布商。
必要條件
- 已整合 IMA SDK 的 iOS 應用程式。
- 設定為傳回隨播廣告的廣告代碼。
實用入門資訊
如果您仍需在應用程式中導入 IMA SDK,請參閱「開始使用」指南。
在應用程式中加入隨播廣告
為隨播廣告素材建立 UIView
要求使用智慧助理前,請先在版面配置中建立智慧助理的空間。在腳本中,將「View」拖曳到 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 現在支援自動調整大小的隨播廣告。這些隨播廣告可調整大小,以符合廣告版位大小。這些範本會填滿上層檢視區塊的寬度,然後調整高度以配合隨播廣告的內容。在 Ad Manager 中使用Fluid
隨播廣告大小設定。請參閱下圖,瞭解如何設定這個值。
更新 iOS 應用程式,支援流暢的隨機存取功能
如要宣告隨播廣告的彈性版位,請只使用 view
參數啟動 IMACompanionAdSlot
,並排除 width
和 height
。
ViewController.m
self.companionSlot =
[[IMACompanionAdSlot alloc] initWithView:self.companionView];
常見問題
- 我按照指南操作,但沒有看到隨播廣告。該怎麼辦?
- 首先,請檢查代碼是否確實傳回隨播廣告。如要這麼做,請在網路瀏覽器中開啟代碼,然後尋找 CompanionAds 代碼。如果看到這類訊息,請檢查傳回的隨播廣告大小是否與您嘗試顯示廣告的 UIView 大小相同。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間: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."]]