感谢大佬提供这么🐮的插件,传送门https://github.dev/FlutterAds/flutter_qq_ads

安装依赖

  1. flutter_qq_ads: ^2.5.0

依赖安装后,尝试运行,如果出现了各种错误,按以下文档对安卓和ios项目做一点简单的配置修改

android

修改android/app/build.gradle

  • compileSdkVersion
  • minSdkVersion
  • targetSdkVersion
  • multiDexEnabled

如下示例,其中...为省略部分

  1. ...
  2. android {
  3. compileSdkVersion 31
  4. ...
  5. defaultConfig {
  6. ...
  7. minSdkVersion 19
  8. targetSdkVersion 31
  9. multiDexEnabled = true
  10. ...
  11. }
  12. ...
  13. }
  14. ...
  15. dependencies {
  16. ...
  17. implementation 'androidx.multidex:multidex:2.0.1'
  18. ...
  19. }

修改android/build.gradle

  • ext.kotlin_version

如下示例,其中...为省略部分

  1. buildscript {
  2. ext.kotlin_version = '1.5.31'
  3. ...
  4. }

修改android/src/main/AndroidManifest.xml

  • 添加联网权限 <uses-permission android:name="android.permission.INTERNET"/>
  • application添加 androidx.multidex.MultiDexApplication

如下示例,其中...为省略部分

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="work.tool.flutter_pet_translate">
  3. <uses-permission android:name="android.permission.INTERNET"/>
  4. <application
  5. android:name="androidx.multidex.MultiDexApplication"
  6. android:label="..."
  7. android:icon="...">
  8. ...
  9. ...

配置修改完成后再次尝试运行,不出意外就成功了。如果仍然有报错,按错误信息去stackoverflow或者github issue搜索,相信会找到相关的解决方案。
胜利就在眼前,千万不要放弃。

iOS

修改ios/Runner/Info.plist

  • 信任HTTP请求 <key>NSAppTransportSecurity</key>

    1. <key>NSAppTransportSecurity</key>
    2. <dict>
    3. <key>NSAllowsArbitraryLoads</key>
    4. <true/>
    5. </dict>
  • 应用跟踪透明度授权 NSUserTrackingUsageDescription

    1. <key>NSUserTrackingUsageDescription</key>
    2. <string>为了向您提供更优质、安全的个性化服务及内容,需要您允许使用相关权限</string>

    在项目中调用

    参考插件的example,创建 ads_config.dartads.dart

  • 配置类ads_config.dart,主要用来存放优量汇媒体ID和广告ID,针对不同的操作系统,使用对应的ID,以开屏广告位ID为例

    1. static String get splashId {
    2. if (Platform.isAndroid) {
    3. return '8022311121246224';
    4. } else {
    5. return '5052818319908354';
    6. }
    7. }
  • 简单封装操作类ads.dart,为了调试方便,使用logger将调用结果做了输出,没有logger的话可以直接删掉相关代码 ```shell import ‘package:flutter/services.dart’; import ‘package:flutter_qq_ads/flutter_qq_ads.dart’;

import ‘ads_config.dart’; import ‘../common/logger.dart’;

var ads = Ads();

class Ads { // 结果信息 String _result = ‘’;

/// 设置广告监听 Future setAdEvent(event) async { logger.info(‘设置成功’); FlutterQqAds.onEventListener((event) { if (event is AdErrorEvent) { // 错误事件 logger.info(‘ errCode:${event.errCode} errMsg:${event.errMsg}’); } else if (event is AdRewardEvent) { logger.info( ‘ transId:${event.transId} customData:${event.customData} userId:${event.userId}’); } logger.info(‘onEventListener:$event’); }); }

/// 请求应用跟踪透明度授权 Future requestIDFA() async { bool result = await FlutterQqAds.requestIDFA; logger.info(‘请求广告标识符:$result’); }

/// 展示插屏广告 Future showInterstitialAd( String posId, { bool showFullScreenVideo = false, bool showRewardVideo = false, }) async { try { bool result = await FlutterQqAds.showInterstitialAd( posId, showPopup: false, showFullScreenVideo: showFullScreenVideo, showRewardVideo: showRewardVideo, autoPlayMuted: false, autoPlayOnWifi: false, detailPageMuted: false, userId: ‘userId’, customData: ‘showInterstitialAd customData’, ); _result = “展示插屏广告${result ? ‘成功’ : ‘失败’}”; } on PlatformException catch (e) { _result = “展示插屏广告失败 code:${e.code} msg:${e.message} details:${e.details}”; } logger.info(_result); }

/// 展示激励视频广告 Future showRewardVideoAd() async { try { bool result = await FlutterQqAds.showRewardVideoAd( AdsConfig.rewardVideoId, playMuted: false, customData: ‘showRewardVideoAd customData’, userId: ‘userId’, ); _result = “展示激励视频广告${result ? ‘成功’ : ‘失败’}”; } on PlatformException catch (e) { _result = “展示激励视频广告失败 code:${e.code} msg:${e.message} details:${e.details}”; } logger.info(_result); }

/// 初始化广告 SDK Future init() async { bool result = false; try { result = await FlutterQqAds.initAd(AdsConfig.appId); _result = “广告SDK 初始化${result ? ‘成功’ : ‘失败’}”; } on PlatformException catch (e) { _result = “广告SDK 初始化失败 code:${e.code} msg:${e.message} details:${e.details}”; } logger.info(_result); return result; }

/// 展示开屏广告 /// [logo] 展示如果传递则展示logo,不传递不展示 Future showSplashAd([String? logo]) async { try { bool result = await FlutterQqAds.showSplashAd( AdsConfig.splashId, logo: logo, fetchDelay: 3, ); _result = “展示开屏广告${result ? ‘成功’ : ‘失败’}”; } on PlatformException catch (e) { _result = “展示开屏广告失败 code:${e.code} msg:${e.message} details:${e.details}”; } logger.info(_result); } }

  1. <a name="qt4e4"></a>
  2. #### 调用
  3. ```shell
  4. @override
  5. void initState() {
  6. super.initState();
  7. if (Platform.isIOS) {
  8. ads.requestIDFA().then((value) {});
  9. }
  10. ads.init().then((value) {
  11. if (value) {
  12. ads.showSplashAd(AdsConfig.logo);
  13. }
  14. });
  15. }