从 v3 升级到 v4
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
IMA DAI SDK 和 IMA 客户端 SDK 已在版本 4 中合并,
并已重新构建为 iOS SDK 的一个精确子集。这与
有助于缩短 iOS 开发者的上手难度。因此,一些代码
DAI 用户所需的内容发生了变化,以便与我们的其他 SDK 更加一致。
本指南详细介绍了升级现有 v3 所需的流程
v4 SDK 的新实现。
如有疑问,请查阅
iOS DAI 示例 -
tvOS v4 API 相同(但随播广告和画中画除外,这两项内容均不适用于
tvOS)。
更改模块名称
为了与 iOS SDK 保持一致,我们已将模块名称从 InteractiveMediaAds
更改为
至 GoogleInteractiveMediaAds
。
变更 |
旧优惠 |
#import <InteractiveMediaAds/InteractiveMediaAds.h>
|
新 |
#import <GoogleInteractiveMediaAds/GoogleInteractiveMediaAds.h> |
旧优惠 |
@import InteractiveMediaAds; |
新 |
@import GoogleInteractiveMediaAds; |
创建新的广告容器
IMAAdDisplayContainer
负责管理广告容器视图和
随播广告位。
变更 |
旧优惠 |
没有先前的等效项。 |
新 |
self.adDisplayContainer =
[[IMAAdDisplayContainer alloc] initWithAdContainer:self.videoView];
|
将 IMAVideoDisplay 和 IMAAdDisplayContainer 传递到 IMAStreamRequest 中,
现在,您已经有了视频显示组件和 IMAAdDisplayContainer,
,需要通过
添加到视频流请求,以便 IMA DAI SDK 可以对其进行管理。
变更 |
旧优惠 |
IMALiveStreamRequest *streamRequest =
[[IMALiveStreamRequest alloc] initWithAssetKey:kAssetKey];
IMAVODStreamRequest *streamRequest =
[[IMAVODStreamRequest alloc] initWithContentSourceID:kContentSourceID
videoID:kVideoID];
|
新 |
IMALiveStreamRequest *streamRequest =
[[IMALiveStreamRequest alloc] initWithAssetKey:kAssetKey
adDisplayContainer:self.adDisplayContainer
videoDisplay:self.videoDisplay];
IMAVODStreamRequest *streamRequest =
[[IMAVODStreamRequest alloc] initWithContentSourceId:kContentSourceID
videoId:kVideoID
adDisplayContainer:self.adDisplayContainer
videoDisplay:self.videoDisplay];
|
使用 IMAAdsLoader 发送请求
变更 |
旧优惠 |
self.streamManager =
[[IMAStreamManager alloc] initWithVideoDisplay:self.videoDisplay];
self.streamManager.delegate = self;
[self.streamManager requestStream:streamRequest];
|
新 |
self.adsLoader = [[IMAAdsLoader alloc] init];
self.adsLoader.delegate = self;
[self.adsLoader requestStreamWithRequest:streamRequest];
|
实现 IMAAdsLoaderDelegate 以初始化视频流
为了与 iOS 版
SDK。直播管理器与直播之间的关系也发生了变化。在
v3 SDK,那么您可以使用单个视频流管理器来管理多个视频流。在 v4 中,
每个视频流管理器只能管理一个视频流。
变更 |
旧优惠 |
- (void)streamManager:(IMAStreamManager *)streamManager
didInitializeStream:(NSString *)streamID {
NSLog(@"Stream initialized with streamID: %@", streamID);
}
- (void)streamManager:(IMAStreamManager *)streamManager
didReceiveError:(NSError *)error {
NSLog(@"Error: %@", error);
[self playBackupStream];
}
|
新 |
- (void)adsLoader:(IMAAdsLoader *)loader
adsLoadedWithData:(IMAAdsLoadedData *)adsLoadedData {
self.streamManager = adsLoadedData.streamManager;
self.streamManager.delegate = self;
[self.streamManager initializeWithAdsRenderingSettings:nil];
NSLog(@"Stream initialized with streamID: %@", self.streamManager.streamId);
}
- (void)adsLoader:(IMAAdsLoader *)loader
failedWithErrorData:(IMAAdLoadingErrorData *)adErrorData {
NSLog(@"Error: %@", adErrorData.adError);
[self playBackupStream];
}
|
实现 IMAStreamManagerDelegate
为了与 iOS SDK 保持一致,tvOS SDK 现在提供单个串流
管理器委托 IMAStreamManagerDelegate
,用于处理流事件。您
现在需要在该委托中使用 switch 语句来管理特定的
事件。
变更 |
旧优惠 |
- (void)streamManager:(IMAStreamManager *)streamManager
adBreakDidStart:(IMAAdBreakInfo *)adBreakInfo {
NSLog(@"Ad break started");
self.playerViewController.requiresLinearPlayback = YES;
}
- (void)streamManager:(IMAStreamManager *)streamManager
adBreakDidEnd:(IMAAdBreakInfo *)adBreakInfo {
NSLog(@"Ad break ended");
self.playerViewController.requiresLinearPlayback = NO;
} |
新 |
- (void)streamManager:(IMAStreamManager *)streamManager
didReceiveAdEvent:(IMAAdEvent *)event {
NSLog(@"StreamManager event (%@).", event.typeString);
switch (event.type) {
case kIMAAdEvent_AD_BREAK_STARTED: {
NSLog(@"Ad break started");
self.playerViewController.requiresLinearPlayback = YES;
break;
}
case kIMAAdEvent_AD_BREAK_ENDED: {
NSLog(@"Ad break ended");
self.playerViewController.requiresLinearPlayback = NO;
break;
}
// And so on for other events.
default:
break;
}
}
|
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eThe IMA DAI SDK v4 for tvOS is now an exact subset of the iOS SDK, simplifying development for iOS developers working on tvOS.\u003c/p\u003e\n"],["\u003cp\u003eUpgrading to v4 requires changing the module name to \u003ccode\u003eGoogleInteractiveMediaAds\u003c/code\u003e, creating an \u003ccode\u003eIMAAdDisplayContainer\u003c/code\u003e, and passing it along with \u003ccode\u003eIMAVideoDisplay\u003c/code\u003e to the stream request.\u003c/p\u003e\n"],["\u003cp\u003eInstead of \u003ccode\u003eIMAStreamManager\u003c/code\u003e, use \u003ccode\u003eIMAAdsLoader\u003c/code\u003e to request streams and implement \u003ccode\u003eIMAAdsLoaderDelegate\u003c/code\u003e for stream initialization.\u003c/p\u003e\n"],["\u003cp\u003eHandle stream events through the unified \u003ccode\u003eIMAStreamManagerDelegate\u003c/code\u003e using a switch statement to manage specific events like ad breaks.\u003c/p\u003e\n"]]],["Version 4 merges the IMA DAI and client-side SDKs, aligning with the iOS SDK. Key changes include: renaming the module to `GoogleInteractiveMediaAds`, creating an `IMAAdDisplayContainer` for ad management, and passing it with `IMAVideoDisplay` to `IMAStreamRequest`. The `IMAAdsLoader` replaces direct stream management, using `IMAAdsLoaderDelegate` for initialization and `IMAStreamManagerDelegate` for events, requiring a switch statement for specific events. Each stream manager now handles only one stream.\n"],null,["# Upgrade from v3 to v4\n\nThe IMA DAI SDK and the IMA client-side SDK have been merged in version 4,\nand have been reworked as an exact subset of the iOS SDK. This significantly\nreduces the learning curve for iOS developers. As a result, some of the code\nrequired for DAI users has changed to be more consistent with our other SDKs.\n\nThis guide walks through the process required to upgrade an existing v3\nimplementation to the new v4 SDK.\n\nIf in doubt, consult the\n[iOS DAI samples](//github.com/googleads/googleads-ima-ios-dai)---the\ntvOS v4 API is the same (except for companions and PIP, which are unavailable on\ntvOS).\n\nChange the module name\n----------------------\n\nTo match the iOS SDK, we've changed the module name from `InteractiveMediaAds`\nto `GoogleInteractiveMediaAds`.\n\n| Changes ||\n|-----|-------------------------------------------------------------------------------|\n| Old | ```python #import \u003cInteractiveMediaAds/InteractiveMediaAds.h\u003e ``` |\n| New | ```python #import \u003cGoogleInteractiveMediaAds/GoogleInteractiveMediaAds.h\u003e ``` |\n| Old | ```python @import InteractiveMediaAds; ``` |\n| New | ```python @import GoogleInteractiveMediaAds; ``` |\n\nCreate the new ad container\n---------------------------\n\nThe `IMAAdDisplayContainer` is responsible for managing the ad container view and\ncompanion ad slots used for ad playback.\n\n| Changes ||\n|-----|------------------------------------------------------------------------------------------------------------------|\n| Old | There is no prior equivalent. |\n| New | ```objective-c self.adDisplayContainer = [[IMAAdDisplayContainer alloc] initWithAdContainer:self.videoView]; ``` |\n\nPass the IMAVideoDisplay and IMAAdDisplayContainer into the IMAStreamRequest\n----------------------------------------------------------------------------\n\nNow that you have a video display and `IMAAdDisplayContainer,` you need to pass\nthem to the stream request so that the IMA DAI SDK can manage them.\n\n| Changes ||\n|-----|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Old | ```objective-c IMALiveStreamRequest *streamRequest = [[IMALiveStreamRequest alloc] initWithAssetKey:kAssetKey]; IMAVODStreamRequest *streamRequest = [[IMAVODStreamRequest alloc] initWithContentSourceID:kContentSourceID videoID:kVideoID]; ``` |\n| New | ```objective-c IMALiveStreamRequest *streamRequest = [[IMALiveStreamRequest alloc] initWithAssetKey:kAssetKey adDisplayContainer:self.adDisplayContainer videoDisplay:self.videoDisplay]; IMAVODStreamRequest *streamRequest = [[IMAVODStreamRequest alloc] initWithContentSourceId:kContentSourceID videoId:kVideoID adDisplayContainer:self.adDisplayContainer videoDisplay:self.videoDisplay]; ``` |\n\nRequest with an IMAAdsLoader\n----------------------------\n\n| Changes ||\n|-----|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Old | ```objective-c self.streamManager = [[IMAStreamManager alloc] initWithVideoDisplay:self.videoDisplay]; self.streamManager.delegate = self; [self.streamManager requestStream:streamRequest]; ``` |\n| New | \u003cbr /\u003e ```objective-c self.adsLoader = [[IMAAdsLoader alloc] init]; self.adsLoader.delegate = self; [self.adsLoader requestStreamWithRequest:streamRequest]; ``` |\n\nImplement IMAAdsLoaderDelegate for stream initialization\n--------------------------------------------------------\n\nThese functions have been renamed and modified to be consistent with the iOS\nSDK. The relationship between stream manager and stream has also changed. In the\nv3 SDK, a single stream manager could be used to manage multiple streams. In v4,\neach stream manager can only manage a single stream.\n\n| Changes ||\n|-----|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Old | ```objective-c - (void)streamManager:(IMAStreamManager *)streamManager didInitializeStream:(NSString *)streamID { NSLog(@\"Stream initialized with streamID: %@\", streamID); } - (void)streamManager:(IMAStreamManager *)streamManager didReceiveError:(NSError *)error { NSLog(@\"Error: %@\", error); [self playBackupStream]; } ``` |\n| New | ```objective-c - (void)adsLoader:(IMAAdsLoader *)loader adsLoadedWithData:(IMAAdsLoadedData *)adsLoadedData { self.streamManager = adsLoadedData.streamManager; self.streamManager.delegate = self; [self.streamManager initializeWithAdsRenderingSettings:nil]; NSLog(@\"Stream initialized with streamID: %@\", self.streamManager.streamId); } - (void)adsLoader:(IMAAdsLoader *)loader failedWithErrorData:(IMAAdLoadingErrorData *)adErrorData { NSLog(@\"Error: %@\", adErrorData.adError); [self playBackupStream]; } ``` |\n\nImplement IMAStreamManagerDelegate\n----------------------------------\n\nFor consistency with the iOS SDKs, the tvOS SDK now provides a single stream\nmanager delegate, `IMAStreamManagerDelegate`, for handling stream events. You\nnow need to use a switch statement within that delegate to manage specific\nevents.\n\n| Changes ||\n|-----|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Old | ```objective-c - (void)streamManager:(IMAStreamManager *)streamManager adBreakDidStart:(IMAAdBreakInfo *)adBreakInfo { NSLog(@\"Ad break started\"); self.playerViewController.requiresLinearPlayback = YES; } - (void)streamManager:(IMAStreamManager *)streamManager adBreakDidEnd:(IMAAdBreakInfo *)adBreakInfo { NSLog(@\"Ad break ended\"); self.playerViewController.requiresLinearPlayback = NO; } ``` |\n| New | ```objective-c - (void)streamManager:(IMAStreamManager *)streamManager didReceiveAdEvent:(IMAAdEvent *)event { NSLog(@\"StreamManager event (%@).\", event.typeString); switch (event.type) { case kIMAAdEvent_AD_BREAK_STARTED: { NSLog(@\"Ad break started\"); self.playerViewController.requiresLinearPlayback = YES; break; } case kIMAAdEvent_AD_BREAK_ENDED: { NSLog(@\"Ad break ended\"); self.playerViewController.requiresLinearPlayback = NO; break; } // And so on for other events. default: break; } } ``` |"]]