迁移到版本 8 指南
插件 8.0.0 移除了与新版原生 SDK 不兼容的部分回调与 API。本说明列出需要在代码中进行的修改。
1. 已移除的 BannerView 回调
以下 BannerView 属性已移除且不再支持:
| 属性 | 平台 |
|---|---|
onLeftApplication |
iOS、Android |
onWillPresentScreen |
iOS |
onDidDismissScreen |
iOS |
onReturnToApplication |
Android |
onAdClose |
iOS、Android |
**处理方式:**在所有使用 BannerView 的地方删除这些属性。
迁移前
<BannerView
adRequest={{ adUnitId: 'R-M-XXXXX-YY' }}
size={adSize}
onAdLoaded={() => console.log('loaded')}
onAdClicked={() => console.log('clicked')}
onLeftApplication={() => console.log('left app')}
onWillPresentScreen={() => console.log('will present')}
onDidDismissScreen={() => console.log('did dismiss')}
onReturnToApplication={() => console.log('returned')}
onAdClose={() => console.log('closed')}
onAdImpression={(e) => console.log('impression', e)}
onAdFailedToLoad={(e) => console.log('failed', e)}
/>
迁移后
<BannerView
adRequest={{ adUnitId: 'R-M-XXXXX-YY' }}
size={adSize}
onAdLoaded={() => console.log('loaded')}
onAdClicked={() => console.log('clicked')}
onAdImpression={(e) => console.log('impression', e)}
onAdFailedToLoad={(e) => console.log('failed', e)}
/>
2. 用 AdRequest 替代 AdRequestConfiguration
AdRequestConfiguration 已移除。请将请求参数(含 adUnitId)以 AdRequestParams 形式直接传给 loadAd。
处理方式:
- 删除对
AdRequestConfiguration的导入与构造; - 将
adUnitId及其他字段直接传入loadAd。
迁移前
import { InterstitialAdLoader, AdRequestConfiguration, Gender } from '@yandex-ads/react-native-yandex-ads';
const loader = await InterstitialAdLoader.create();
const config = new AdRequestConfiguration({
adUnitId: 'R-M-XXXXX-YY',
age: '25',
gender: Gender.Male,
});
const ad = await loader.loadAd(config);
迁移后
import { InterstitialAdLoader, Gender } from '@yandex-ads/react-native-yandex-ads';
const loader = await InterstitialAdLoader.create();
const ad = await loader.loadAd({
adUnitId: 'R-M-XXXXX-YY',
targeting: {
age: '25',
gender: Gender.Male,
},
});
RewardedAdLoader 与 AppOpenAdLoader 同样适用。
3. 将 adUnitId 从 BannerView 属性移至 adRequest
BannerView 不再提供 adUnitId 属性,广告位 ID 须作为 adRequest 的必填字段传入。
**处理方式:**删除 adUnitId 属性,并在 adRequest.adUnitId 中设置。
迁移前
<BannerView
adUnitId="R-M-XXXXX-YY"
size={adSize}
/>
迁移后
<BannerView
size={adSize}
adRequest={{ adUnitId: 'R-M-XXXXX-YY' }}
/>
4. 将定向参数放入 targeting 对象
全屏格式与 BannerView 中,age、gender、location、contextQuery、contextTags 均置于 targeting 下。
**处理方式:**将上述字段包在 targeting: { ... } 中。
全屏广告(插屏、激励、开屏)
迁移前
loader.loadAd({
adUnitId: 'R-M-XXXXX-YY',
age: '25',
gender: Gender.Male,
contextTags: ['sport', 'news'],
});
迁移后
loader.loadAd({
adUnitId: 'R-M-XXXXX-YY',
targeting: {
age: '25',
gender: Gender.Male,
contextTags: ['sport', 'news'],
},
});
BannerView
迁移前
import { Gender } from '@yandex-ads/react-native-yandex-ads';
<BannerView
adUnitId="R-M-XXXXX-YY"
size={adSize}
adRequest={{ age: '25', gender: Gender.Male }}
/>
迁移后
import { Gender } from '@yandex-ads/react-native-yandex-ads';
<BannerView
size={adSize}
adRequest={{ adUnitId: 'R-M-XXXXX-YY', targeting: { age: '25', gender: Gender.Male } }}
/>
5. 新增 AdInfo 接口
8.0.0 新增 AdInfo,用于描述已加载广告的元数据。
interface AdInfo {
adUnitId: string;
extraData?: string;
partnerText?: string;
creatives: Creative[];
}
interface Creative {
creativeId?: string;
campaignId?: string;
placeId?: string;
offerId?: string;
}
全屏格式中的 AdInfo
InterstitialAd、RewardedAd、AppOpenAd 提供 adInfo 取值属性:
const ad = await loader.loadAd({ adUnitId: 'R-M-XXXXX-YY' });
console.log(ad.adInfo?.adUnitId);
console.log(ad.adInfo?.creatives);
BannerView 中的 AdInfo
onAdLoaded 回调的事件中包含 adInfo:
<BannerView
size={adSize}
adRequest={{ adUnitId: 'R-M-XXXXX-YY' }}
onAdLoaded={(event) => {
const adInfo = event.nativeEvent.adInfo;
console.log(adInfo?.adUnitId);
}}
/>
6. 版本要求
iOS
- Xcode:26.0 或更高。
- AppMetricaCore:6.0.0 或更高。
- AppMetricaLibraryAdapter:6.0.0 或更高。
- AppMetricaAdSupport:6.0.0 或更高。
- AppMetricaIDSync:6.0.0 或更高。
Android
- Gradle:7.0 或更高。
- Android Gradle Plugin:8.3.2 或更高。
- Android SDK:API 21(Android 5.0)或更高。
- Kotlin:1.9.0 或更高。
- Java:8 或更高。