Flutter广告集成插件yandex_mobileads_v2的使用

Flutter广告集成插件yandex_mobileads_v2的使用

简介

Flutter插件yandex_mobileads_v2允许开发者将Yandex Mobile Ads SDK集成到Android和iOS应用中。通过该插件,开发者可以轻松地在Flutter应用中实现广告功能。


使用步骤

1. 配置 pubspec.yaml

首先,在pubspec.yaml文件中添加依赖项:

dependencies:
  yandex_mobileads_v2: ^7.5.0

运行以下命令以安装依赖:

flutter pub get

2. Android 配置

添加依赖

android/app/build.gradle文件中添加以下依赖:

推荐方式(包含所有适配器):

dependencies {
    // 其他依赖...
    implementation 'com.yandex.android:mobileads-mediation:7.6.0.0'
}

手动选择适配器的方式:

dependencies {
    // 其他依赖...
    implementation 'com.yandex.android:mobileads:7.6.0'
    implementation 'com.yandex.ads.mediation:mobileads-google:23.4.0.0'
    implementation 'com.yandex.ads.mediation:mobileads-inmobi:10.7.8.0'
    implementation 'com.yandex.ads.mediation:mobileads-mytarget:5.22.1.2'
    implementation 'com.yandex.ads.mediation:mobileads-startapp:5.0.2.3'
    implementation 'com.yandex.ads.mediation:mobileads-unityads:4.12.3.0'
    implementation 'com.yandex.ads.mediation:mobileads-applovin:12.6.0.3'
    implementation 'com.yandex.ads.mediation:mobileads-ironsource:8.4.0.0'
    implementation 'com.yandex.ads.mediation:mobileads-chartboost:9.3.1.11'
    implementation 'com.yandex.ads.mediation:mobileads-pangle:6.2.0.7.0'
    implementation 'com.yandex.ads.mediation:mobileads-tapjoy:13.4.1.6'
    implementation 'com.yandex.ads.mediation:mobileads-vungle:7.4.1.0'
    implementation 'com.yandex.ads.mediation:mobileads-mintegral:16.8.61.0'
    implementation 'com.yandex.ads.mediation:mobileads-bigoads:5.0.1.0'
}

添加 AdMob ID(如果使用 AdMob)

AndroidManifest.xml中添加AdMob ID:

<manifest>
    <application>
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
    </application>
</manifest>

添加 Maven URLs

android/build.gradle中添加以下Maven仓库:

allprojects {
    repositories {
        // 其他配置...
        // IronSource
        maven { url 'https://android-sdk.is.com/' }
        // Pangle
        maven { url 'https://artifact.bytedance.com/repository/pangle' }
        // Tapjoy
        maven { url "https://sdk.tapjoy.com/" }
        // Mintegral
        maven { url "https://dl-maven-android.mintegral.com/repository/mbridge_android_sdk_oversea" }
        // Chartboost
        maven { url "https://cboost.jfrog.io/artifactory/chartboost-ads/" }
        // AppNext
        maven { url "https://dl.appnext.com/" }
    }
}

3. iOS 配置

使用 CocoaPods

ios/Podfile中添加以下依赖:

推荐方式(包含所有适配器):

pod 'YandexMobileAdsMediation', '~> 7.6.0'

手动选择适配器的方式:

pod 'YandexMobileAds', '~> 7.6.0'
pod 'GoogleYandexMobileAdsAdapters', '11.10.0.0'
pod 'InMobiYandexMobileAdsAdapters', '10.7.8.0'
pod 'MyTargetYandexMobileAdsAdapters', '5.21.7.3'
pod 'StartAppYandexMobileAdsAdapters', '4.10.4.4'
pod 'UnityAdsYandexMobileAdsAdapters', '4.12.3.0'
pod 'AppLovinYandexMobileAdsAdapters', '12.6.1.3'
pod 'IronSourceYandexMobileAdsAdapters', '8.4.0.0'
pod 'MintegralYandexMobileAdsAdapters', '7.7.3.0'
pod 'AdColonyYandexMobileAdsAdapters', '4.9.0.21'
pod 'ChartboostYandexMobileAdsAdapters', '9.7.0.13'
pod 'BigoADSYandexMobileAdsAdapters', '4.2.3.10'

运行以下命令以更新Pod:

cd ios
pod install

添加 AdMob ID

ios/Info.plist中添加AdMob ID:

<key>GADApplicationIdentifier</key>
<string>ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy</string>

添加 SKAdNetworkIdentifiers

同样在ios/Info.plist中添加以下内容:

<key>SKAdNetworkItems</key>
<array>
    <!-- Yandex Ads -->
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>zq492l623r.skadnetwork</string>
    </dict>
    <!-- Google (ex. AdMob) -->
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>cstr6suwn9.skadnetwork</string>
    </dict>
    <!-- VK Реклама (ex. myTarget) -->
    <dict>
        <key>SKAdNetworkIdentifier</key>
        <string>n9x2a789qt.skadnetwork</string>
    </dict>
    <!-- 其他网络适配器 -->
    <!-- ... -->
</array>

4. 初始化广告

在Flutter应用中初始化广告。例如,创建一个简单的广告展示页面:

import 'package:flutter/material.dart';
import 'package:yandex_mobileads_v2/yandex_mobileads_v2.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Yandex Mobile Ads Example')),
        body: Center(
          child: YandexBannerAd(
            adUnitId: 'test_BANNER',
            sizes: [
              AdSize.banner,
            ],
            listener: (event) {
              print('Event: $event');
            },
          ),
        ),
      ),
    );
  }
}

完整示例代码

以下是一个完整的示例,展示了如何在Flutter中集成Yandex广告:

import 'package:flutter/material.dart';
import 'package:yandex_mobileads_v2/yandex_mobileads_v2.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Yandex Mobile Ads Example')),
        body: Center(
          child: YandexBannerAd(
            adUnitId: 'test_BANNER',
            sizes: [
              AdSize.banner,
            ],
            listener: (event) {
              print('Event: $event');
            },
          ),
        ),
      ),
    );
  }
}

更多关于Flutter广告集成插件yandex_mobileads_v2的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter广告集成插件yandex_mobileads_v2的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


yandex_mobileads_v2 是一个用于在 Flutter 应用中集成 Yandex 广告的插件。通过这个插件,你可以轻松地在 Flutter 应用中展示横幅广告、插页式广告、激励视频广告等。

1. 添加依赖

首先,在 pubspec.yaml 文件中添加 yandex_mobileads_v2 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  yandex_mobileads_v2: ^1.0.0  # 请检查最新版本

然后运行 flutter pub get 来安装依赖。

2. 初始化 Yandex Mobile Ads SDK

在你的应用启动时,需要初始化 Yandex Mobile Ads SDK。通常,你可以在 main.dart 文件中进行初始化:

import 'package:flutter/material.dart';
import 'package:yandex_mobileads_v2/yandex_mobileads_v2.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await YandexMobileAds.initialize();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

3. 展示横幅广告

要在应用中展示横幅广告,可以使用 BannerAd 类。以下是一个简单的示例:

import 'package:flutter/material.dart';
import 'package:yandex_mobileads_v2/yandex_mobileads_v2.dart';

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  BannerAd? _bannerAd;

  [@override](/user/override)
  void initState() {
    super.initState();
    _loadBannerAd();
  }

  void _loadBannerAd() {
    _bannerAd = BannerAd(
      adUnitId: 'your_banner_ad_unit_id', // 替换为你的广告单元ID
      size: AdSize.banner,
      onAdLoaded: () {
        print('Banner Ad loaded');
      },
      onAdFailedToLoad: (error) {
        print('Banner Ad failed to load: $error');
      },
    );
    _bannerAd?.load();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Yandex Ads'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Banner Ad Example'),
            if (_bannerAd != null)
              Container(
                alignment: Alignment.center,
                child: AdWidget(ad: _bannerAd!),
                width: _bannerAd!.size.width.toDouble(),
                height: _bannerAd!.size.height.toDouble(),
              ),
          ],
        ),
      ),
    );
  }

  [@override](/user/override)
  void dispose() {
    _bannerAd?.dispose();
    super.dispose();
  }
}

4. 展示插页式广告

插页式广告通常在全屏展示。你可以使用 InterstitialAd 类来加载和展示插页式广告:

import 'package:flutter/material.dart';
import 'package:yandex_mobileads_v2/yandex_mobileads_v2.dart';

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  InterstitialAd? _interstitialAd;

  [@override](/user/override)
  void initState() {
    super.initState();
    _loadInterstitialAd();
  }

  void _loadInterstitialAd() {
    _interstitialAd = InterstitialAd(
      adUnitId: 'your_interstitial_ad_unit_id', // 替换为你的广告单元ID
      onAdLoaded: () {
        print('Interstitial Ad loaded');
      },
      onAdFailedToLoad: (error) {
        print('Interstitial Ad failed to load: $error');
      },
      onAdDismissed: () {
        print('Interstitial Ad dismissed');
        _loadInterstitialAd(); // 重新加载广告
      },
    );
    _interstitialAd?.load();
  }

  void _showInterstitialAd() {
    if (_interstitialAd != null && _interstitialAd!.isLoaded) {
      _interstitialAd?.show();
    } else {
      print('Interstitial Ad is not ready yet.');
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Yandex Ads'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: _showInterstitialAd,
          child: Text('Show Interstitial Ad'),
        ),
      ),
    );
  }

  [@override](/user/override)
  void dispose() {
    _interstitialAd?.dispose();
    super.dispose();
  }
}

5. 展示激励视频广告

激励视频广告通常用于奖励用户。你可以使用 RewardedAd 类来加载和展示激励视频广告:

import 'package:flutter/material.dart';
import 'package:yandex_mobileads_v2/yandex_mobileads_v2.dart';

class MyHomePage extends StatefulWidget {
  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  RewardedAd? _rewardedAd;

  [@override](/user/override)
  void initState() {
    super.initState();
    _loadRewardedAd();
  }

  void _loadRewardedAd() {
    _rewardedAd = RewardedAd(
      adUnitId: 'your_rewarded_ad_unit_id', // 替换为你的广告单元ID
      onAdLoaded: () {
        print('Rewarded Ad loaded');
      },
      onAdFailedToLoad: (error) {
        print('Rewarded Ad failed to load: $error');
      },
      onRewarded: (reward) {
        print('User earned reward: $reward');
      },
      onAdDismissed: () {
        print('Rewarded Ad dismissed');
        _loadRewardedAd(); // 重新加载广告
      },
    );
    _rewardedAd?.load();
  }

  void _showRewardedAd() {
    if (_rewardedAd != null && _rewardedAd!.isLoaded) {
      _rewardedAd?.show();
    } else {
      print('Rewarded Ad is not ready yet.');
    }
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Yandex Ads'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: _showRewardedAd,
          child: Text('Show Rewarded Ad'),
        ),
      ),
    );
  }

  [@override](/user/override)
  void dispose() {
    _rewardedAd?.dispose();
    super.dispose();
  }
}

6. 处理广告生命周期

确保在页面销毁时释放广告资源,以避免内存泄漏。在 dispose 方法中调用 dispose 方法:

[@override](/user/override)
void dispose() {
  _bannerAd?.dispose();
  _interstitialAd?.dispose();
  _rewardedAd?.dispose();
  super.dispose();
}
回到顶部