Flutter广告中介插件gma_mediation_ironsource的使用
Google Mobile Ads Mediation of ironSource for Flutter
This repository contains the source code for the Mediation ironSource Flutter plugin to be used with the Google Mobile Ads plugin. It enables mediation of the ironSource Ad Network through the Google Mobile Ads SDK.
This plugin uses the Pigeon Flutter plugin to generate the classes that bridge the Dart layer with each platform’s code. To add or modify the third-party SDK, use the classes in the pigeons
folder following this guide.
Documentation
For instructions on how to use this plugin with the google_mobile_ads
plugin, refer to the developer guide for ironSource.
Downloads
See pub.dev for the latest releases of the plugin.
Suggesting Improvements
To file bugs, make feature requests, or to suggest other improvements, please use github’s issue tracker.
Other Resources
License
This project is licensed under the Apache 2.0 License.
Example Code
Below is a simple example demonstrating how to integrate the gma_mediation_ironsource
plugin into a Flutter application. This example initializes the Google Mobile Ads SDK and loads an interstitial ad.
Step 1: Add Dependencies
Add the following dependencies to your pubspec.yaml
file:
dependencies:
flutter:
sdk: flutter
google_mobile_ads: ^2.0.0
gma_mediation_ironsource: ^1.0.0
Step 2: Initialize the Google Mobile Ads SDK
Initialize the Google Mobile Ads SDK in your main.dart
file:
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
MobileAds.instance.initialize();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
InterstitialAd? _interstitialAd;
@override
void initState() {
super.initState();
_createInterstitialAd();
}
void _createInterstitialAd() {
InterstitialAd.load(
adUnitId: 'ca-app-pub-3940256099942544/1033173712', // Test Ad Unit ID
request: AdRequest(),
adLoadCallback: InterstitialAdLoadCallback(
onAdLoaded: (ad) {
print('$ad loaded');
_interstitialAd = ad;
},
onAdFailedToLoad: (err) {
print('InterstitialAd failed to load: $err');
},
),
);
}
void _showInterstitialAd() {
if (_interstitialAd == null) {
print('Ad not ready yet');
return;
}
_interstitialAd!.fullScreenContentCallback = FullScreenContentCallback(
onAdDismissedFullScreenContent: (ad) {
print('$ad onAdDismissedFullScreenContent');
ad.dispose();
_createInterstitialAd();
},
onAdFailedToShowFullScreenContent: (ad, err) {
print('$ad onAdFailedToShowFullScreenContent: $err');
ad.dispose();
_createInterstitialAd();
},
);
_interstitialAd!.show();
_interstitialAd = null;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: ElevatedButton(
onPressed: _showInterstitialAd,
child: const Text('Show Interstitial Ad'),
),
),
),
);
}
}
Explanation
- Dependencies: Add the necessary dependencies to your
pubspec.yaml
file. - Initialization: Initialize the Google Mobile Ads SDK in the
main.dart
file usingWidgetsFlutterBinding.ensureInitialized()
andMobileAds.instance.initialize()
. - Create Interstitial Ad: Define a method
_createInterstitialAd()
to load an interstitial ad using theInterstitialAd.load()
method. - Show Interstitial Ad: Define a method
_showInterstitialAd()
to show the interstitial ad when the button is pressed. - UI: Create a simple UI with a button that triggers the display of the interstitial ad.
This example demonstrates a basic integration of the gma_mediation_ironsource
plugin with the google_mobile_ads
plugin. You can expand upon this by adding more ad types and handling different ad events as needed.
更多关于Flutter广告中介插件gma_mediation_ironsource的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter广告中介插件gma_mediation_ironsource的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中集成和使用gma_mediation_ironsource
广告中介插件的一个基本示例。这个插件允许你在Flutter应用中通过IronSource中介显示广告。请确保你已经按照官方文档完成了必要的设置,比如注册IronSource开发者账号、创建应用、获取App Key等。
1. 添加依赖
首先,在你的pubspec.yaml
文件中添加gma_mediation_ironsource
依赖:
dependencies:
flutter:
sdk: flutter
gma_mediation_ironsource: ^最新版本号 # 请替换为实际的最新版本号
然后运行flutter pub get
来安装依赖。
2. 配置Android和iOS
Android
在android/app/src/main/AndroidManifest.xml
中添加必要的权限和IronSource的配置:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp">
<!-- 其他配置 -->
<meta-data
android:name="com.ironsource.sdk.android.AppKey"
android:value="你的IronSource App Key" />
<meta-data
android:name="com.ironsource.sdk.android.PlacementNames"
android:value="your_placement_name1,your_placement_name2" /> <!-- 替换为你的Placement名称 -->
<!-- 其他权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
iOS
在ios/Runner/Info.plist
中添加IronSource的配置:
<key>IronSource</key>
<dict>
<key>AppKey</key>
<string>你的IronSource App Key</string>
<key>PlacementNames</key>
<array>
<string>your_placement_name1</string>
<string>your_placement_name2</string> <!-- 替换为你的Placement名称 -->
</array>
</dict>
3. 初始化IronSource
在你的Flutter应用的入口文件(通常是main.dart
)中初始化IronSource:
import 'package:flutter/material.dart';
import 'package:gma_mediation_ironsource/gma_mediation_ironsource.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
// 初始化IronSource
IronSource.instance.init(
appKey: '你的IronSource App Key',
adUnitIds: {
'your_placement_name1': 'placement_id_1',
'your_placement_name2': 'placement_id_2',
},
listener: (IronSourceEvent event) {
// 处理IronSource事件,比如广告加载、展示、点击等
print('IronSourceEvent: ${event.toJson()}');
},
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter IronSource Demo'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
// 请求并展示广告
bool isAdAvailable = await IronSource.instance.isAdAvailable('your_placement_name1');
if (isAdAvailable) {
IronSource.instance.showAd('your_placement_name1');
} else {
print('Ad is not available.');
}
},
child: Text('Show Ad'),
),
),
);
}
}
注意事项
- 权限:确保在Android和iOS项目中正确配置了所有必要的权限。
- 测试:在开发阶段,使用IronSource提供的测试Ad Units和测试设备ID进行测试。
- 真实设备:某些广告网络可能不会在模拟器上展示广告,确保在真实设备上进行测试。
- 事件监听:监听IronSource事件可以帮助你更好地调试和优化广告展示流程。
这个示例展示了如何在Flutter应用中集成IronSource广告中介,并通过按钮触发广告展示。根据具体需求,你可能需要调整配置和代码逻辑。